角色服务"这"正在改变窗口对象的上下文

时间:2014-08-23 17:31:38

标签: javascript angularjs window this angular-services

我确信我以某种方式弄乱了这一点,并且遗漏了一些非常明显的东西。目标是提供一种服务,该服务公开一些基本认证(登录,注销,isLoggedIn)和潜在的授权功能。

当页面加载时,我检查一个cookie并尝试从服务器检索用户,如果会话cookie指示用户已登录。我不会预料到很多整页转换,但我认为有一个-2整页可能是最好的方法,我不想将用户存储在cookie中。

我了解通过将数据/方法附加到此对象来创建服务。我得到了这个上下文改变的promise的内部,为什么在isLoggedIn方法中这个上下文是否引用了window对象?

angular.module('myNgApplication').service('MyAuthentication', function ($cookies, UserProxy) {

    this.user = null ;

    (function(){
        var SESSION_COOKIE = 'loggedIn'
        if($cookies[SESSION_COOKIE]) {
            var self = this
            UserProxy.retrieveSession().then(function(authResponse){
                console.log('init')
                console.log(self)
                self.user = authResponse
            })
        }
    }).call(this)



    this.isLoggedIn = function() {
        console.log('isLoggedIn')
        console.log(this)
        return this.user != null ;
    }

    this.login = function (email, password) {
        var self = this
        return UserProxy.login(email, password).then(function(authResponse){
            self.user = authResponse
            return self.user
        })
    }
})

用法:

var myWelcomeController = function($scope, MyAuthentication, $timeout) {


    $scope.$watch(function(){ return MyAuthentication.user }, function() {
        console.log(MyAuthentication.user)
        $scope.user = MyAuthentication.user ;
        $timeout(MyAuthentication.isLoggedIn, 1000)
    });

};

控制台:

init 
Constructor {user: null, isLoggedIn: function, login: function}

isLoggedIn 
Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}

1 个答案:

答案 0 :(得分:1)

$timeout只是一个围绕javascript setTimeout函数的微小角度包装器,因此行为方式类似。

请考虑以下示例:

var foo = {
  bar: function(){
    console.log('bar this =', this);
  }
};
foo.bar(); //-> bar this = Object {bar: function}
setTimeout(foo.bar); //-> bar this = Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}

当您致电foo.bar()时,javascript运行时将此功能bar的上下文设置为foo。当您致电setTimeout(foo.bar)时,setTimeout函数仅引用bar函数,并将此上下文设置为Window来调用它 - 完全如documentation中所述

最后,您可以通过以下更改使代码正常工作:

$timeout(function(){
  MyAuthentication.isLoggedIn()
}, 1000);