绑定Angular.js中的服务变量

时间:2014-06-26 16:32:24

标签: javascript angularjs angularjs-scope angular-services

我正在尝试构建一个登录模式,它将跨不同的控制器更新模板(只是基本的用户信息 - 头像,名称等)。松散地遵循这个example,我的方法是将服务变量直接绑定在我的部分中,如下所示:

部分:

<div ng-controller="TopBarControl">
    <span>{{ userService.getInfo() }}</span>
</div>

服务:

.service('userService', function($http) {

    this.userInfo = {
        isLogged: false         
    }

    this.getInfo = function() {
        return this.userInfo;
    }

    this.loginInit = function(userName, password) {
        $http.get('http://example.com/?json=get_nonce&controller=auth&method=generate_auth_cookie').success(
        function(data, status, headers, config) {
            var nonce = data.nonce;
            $http.get('http://example.com/?json=auth/generate_auth_cookie&nonce='+nonce+'&username='+userName+'&password='+password).success(function(data, status, headers, config) {
                if (data.status == 'ok') {
                    this.userInfo = [
                        {isLogged: true},
                        {username: data.user.username},
                        {firstName: data.user.firstname},
                        {lastName: data.user.lastname},
                        {avatar: data.user.avatar}
                    ];
                    return userInfo;
                } 
               /* handle errors and stuff*/     
        }); 
    }
})

控制器:

.controller('TopBarControl', function($scope, $modal, userService) {

    $scope.userService = userService;

    $scope.openLogin = function() {
        var modalInstance = $modal.open({
        templateUrl: 'views/modal.html',
        controller: ModalInstanceCtrl,
        size: 'lg'
      });
    }   
});

var ModalInstanceCtrl = function ($scope, $modalInstance, userService) {
  $scope.login = function () {
    userService.loginInit(this.userName, this.password);
  };

  $scope.ok = function () {
    $modalInstance.close($scope.returnResolveVariable);
  };
};

所以,登录工作正常。数据将发送到服务器,并将正确的数据成功恢复到应用程序。双向绑定排序有效 - 每次发生更改时,模板都会回调.getInfo()并吐出userInfo的值。问题是userInfo的值永远不会改变。我无法弄清楚我在loginInit中设置变量的方式是否奇怪,或者我是否从根本上不了解服务如何处理这样的变量。

1 个答案:

答案 0 :(得分:1)

您使用'this'关键字。第一次设置this.userinfo时'this'关键字的上下文与$ http处理方法中的'this'上下文不同。

执行类似这样的操作,捕获'this'关键字的原始上下文,以便日后使用:

.service('userService', function($http) {
    var self = this;

    this.userInfo = {
        isLogged: false         
    };

    this.getInfo = function() {
        return this.userInfo;
    };

    this.loginInit = function(userName, password) {
        $http.get('http://example.com/?json=get_nonce&controller=auth&method=generate_auth_cookie').success(
        function(data, status, headers, config) {
            var nonce = data.nonce;
            $http.get('http://example.com/?json=auth/generate_auth_cookie&nonce='+nonce+'&username='+userName+'&password='+password).success(function(data, status, headers, config) {
                if (data.status == 'ok') {
                   self.userInfo = [
                        {isLogged: true},
                        {username: data.user.username},
                        {firstName: data.user.firstname},
                        {lastName: data.user.lastname},
                        {avatar: data.user.avatar}
                    ];
                    return userInfo;
                } 
               /* handle errors and stuff*/     
        }); 
    };
});

在上面的代码中,userInfo变量将在返回$ http调用时限定为内联处理函数。

理解'this'关键字是非常棘手的,这看起来像是一个很好的介绍'http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/',虽然我倾向于不使用它并使用揭示模块模式来解决这种混乱,看看{here for an example 3}}

相关问题