广播听众有角度地叫了两次

时间:2017-08-11 19:34:30

标签: javascript angularjs

我有一个登录控制器,我正在尝试注册/登录:

app.controller('signupController', function($scope, $sessionStorage, $state, authService){

    $scope.$storage = $sessionStorage;

    $scope.socialAuthentication = function(e){
        authService.login(e);
    };

    $scope.logout = function(e){
        authService.logout();
    };

    $scope.$on('authenticate', function(event, data){
        console.log('user ', data); // prints twice here
        if(data != null && data.access_token != ""){
            $scope.$storage.isLoggedIn = true;
        } else {
            $scope.$storage.isLoggedIn = false;
        }
        $state.go('home');
    });
});

验证服务:

app.service('authService', function($rootScope, $http){

    this.login = function(e){
        var social = e.currentTarget.dataset.social;
        popupTools.popup('/auth/'+social+'/', "Authentication", {}, function (err, user) {
            if (err) {
                console.log(err.message);
                return false;
            } else {
                $rootScope.$broadcast('authenticate', user);
            }
        });
    };

    this.logout = function(){
        $rootScope.$broadcast('authenticate', null);
    };
});

问题是$scope.$on被调用两次打印日志消息两次。为什么会如此,我无法理解。

0 个答案:

没有答案