2016-04-03 111 views
2

我试图让一个函数在用户登录后触发。我尝试在定时器中包装广播,因为我看到有人提示它,但它似乎并不是加工。有些人遇到了问题,即他们的控制器尚未初始化,但我在app.run中有$ on,所以应该已经发生了。

app.run(['$rootScope', '$http', '$cookies', '$cookieStore', function ($rootScope, $http, $cookies, $cookieStore) { 

$rootScope.logout = function() { 

    $http.post('API' + '/api/Account/Logout') 
     .success(function (data, status, headers, config) { 
      $http.defaults.headers.common.Authorization = null; 
      $http.defaults.headers.common.RefreshToken = null; 
      $cookieStore.remove('_Token'); 
      $cookieStore.remove('_RefreshToken'); 
      $rootScope.username = ''; 
      $rootScope.loggedIn = false; 
      window.location = '#/signin'; 
     }); 

} 

$rootScope.$on('loggedIn', function (event) { 
    if ($http.defaults.headers.common.RefreshToken != null) { 
     var params = "grant_type=refresh_token&refresh_token=" + $http.defaults.headers.common.RefreshToken; 
     $http({ 
      url: 'http://localhost:52644/Token', 
      method: "POST", 
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 
      data: params 
     }) 
     .success(function (data, status, headers, config) { 
      $http.defaults.headers.common.Authorization = "Bearer " + data.access_token; 
      $http.defaults.headers.common.RefreshToken = data.refresh_token; 

      $cookieStore.put('_Token', data.access_token); 
      $cookieStore.put('_RefreshToken', data.refresh_token); 

      $http.get('http://localhost:52644/api/Account/GetUserInfo') 
       .success(function (data, status, headers, config) { 
        if (data != "null") { 
         $rootScope.userEmail = data.replace(/["']{1}/gi, "");//Remove any quotes from the username before pushing it out. 
         $rootScope.userFirstName = data.FirstName; 
         $rootScope.loggedIn = true; 
        } 
        else 
         $rootScope.loggedIn = false; 
       }); 


     }) 
     .error(function (data, status, headers, config) { 
      $rootScope.loggedIn = false; 
     }); 
    } 
}); 

}]);

app.controller('signInCtrl', ['$scope', '$rootScope', '$http', '$cookies', '$cookieStore', '$location', '$routeParams', '$uibModalInstance', '$timeout' ,function ($scope, $rootScope, $http, $cookies, $cookieStore, $location, $routeParams, $uibModalInstance, $timeout) { 
$scope.message = $routeParams.message; 
$scope.signIn = function() { 
    $scope.showMessage = false; 
    var params = "grant_type=password&username=" + $scope.username + "&password=" + $scope.password; 
    $http({ 
     url: 'http://localhost:52644/token', 
     method: "POST", 
     headers: { 
      'Content-Type': 'application/x-www-form-urlencoded' 
     }, 
     data: params 
    }) 
    //$http.post('http://localhost:52644/token', params, { 
    // headers: { 
    //  'Content-Type': 'application/x-www-form-urlencoded' 

    // } 
    .success(function (data, status, 
     s, config) { 
     $http.defaults.headers.common.Authorization = "Bearer " + data.access_token; 
     $http.defaults.headers.common.RefreshToken = data.refresh_token; 

     $cookieStore.put('_Token', data.access_token); 
     $rootScope.$broadcast('loggedIn'); 
     $timeout(function() { 
      $rootScope.$broadcast('loggedIn'); 
     }, 100); 
     $uibModalInstance.close(); 
    }) 
    .error(function (data, status, headers, config) { 
     $scope.message = data.error_description.replace(/["']{1}/gi, ""); 
     $scope.showMessage = true; 
    }); 
} 

}]);

回答

2

您不能广播(或一起)$ rootScope事件处理程序。你只能$发出它。

$rootScope.$emit('loggedIn'); 

documentation

$emit(name, args);通过范围 层级通知注册$rootScope.Scope听众向上将事件调度名称。

事件生命周期从$emit被调用的范围开始。 收听此范围内名称事件的所有侦听器都会收到通知。 之后,事件向上遍历根范围,并且 一路调用所有已注册的侦听器。如果其中一位听众取消了该活动,则该活动将停止传播 。

+0

我实际上尝试使用发射第一,这似乎并没有工作。 – Rich

+0

@然后,您需要创建一个小型重击器或小提琴来重现问题。在AngularJS中发生事件有一些非常严格和快速的规则,所以如果这不能解决你的问题,你的代码中很可能会有另一个问题,在你的问题中不明显。 –