【问题标题】:Binding Service Variables in Angular.js在 Angular.js 中绑定服务变量
【发布时间】:2014-06-27 00:50:19
【问题描述】:

我正在尝试构建一个登录模式,它将跨不同控制器更新模板(只是基本的用户信息 - 头像、姓名等)。大致遵循这个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 中设置变量的方式是否有些奇怪,或者我根本不了解服务如何处理这样的变量。

【问题讨论】:

  • 在您的示例中,控制器的 userInfo 和服务的 userInfo 变量之间的关系在哪里。它们不是同一个变量。
  • 哎呀...被评论了 - 它只是我想要生成的数组的参考。为了清楚起见,我删除了它。

标签: javascript angularjs angularjs-scope angular-services


【解决方案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

【讨论】:

  • 哇,成功了。谢谢。
  • 别担心,JavaScript 是一个棘手的东西,但你会越来越喜欢它!
猜你喜欢
  • 2013-05-18
  • 2019-01-04
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2019-03-05
相关资源
最近更新 更多