我怎么在控制器声明$ rootscope和访问它的另一个控制器 [英] how do I declare a $rootscope in a controller and access it in another controller

查看:93
本文介绍了我怎么在控制器声明$ rootscope和访问它的另一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现一个角资源的登录系统这样

I am implementing an angular resource for a login system like this

.. factory.js

..factory.js

//Create a factory for the Login API
angular.module(...)
.factory('LoginEntity', ['$resource', function ($resource) {
return $resource(
    'API/sigin',
    {
        save: {method: 'POST'},
        update: {method: 'PUT'}
    }
);
}])

... controller.js

...controller.js

.controller('xxxController', ['LoginEntity', function(LoginEntity, $rootScope){
//in Controller add LoginEntity dependency
  $rootScope.loggedinuser = {};

LoginEntity.save({
                        username: user.userName,
                        password: user.password
                    },
                    function (response) {
                        //success callback
                      $rootScope.loggedinuser = response;
                    },
                    function () {
                        //error callback

                    });

}]);

... AppCtrl

...AppCtrl

.controller('AppCtrl',function($scope,$rootScope){

   username = $rootScope.loggedinuser.username;

  }

这是我,但rootscope似乎并没有成为在App控制器入店。

That is what I have but the rootscope does not seem to be accesible in the App controller.

任何帮助,或协助将pciated高度AP $ P $

Any help, or assistance will be highly appreciated

推荐答案

您controller.js有错误,请将 $ rootScope 您code这样的

your controller.js has a error, add the $rootScope to your code like this:

.controller('xxxController', ['LoginEntity', '$rootScope', function(LoginEntity, $rootScope){
//in Controller add LoginEntity dependency
  $rootScope.loggedinuser = {};

LoginEntity.save({
                        username: user.userName,
                        password: user.password
                    },
                    function (response) {
                        //success callback
                      $rootScope.loggedinuser = response;
                    },
                    function () {
                        //error callback

                    });

}]);

这篇关于我怎么在控制器声明$ rootscope和访问它的另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆