我正试图在用户登录后启动一个函数。我试图将广播封装在计时器中,因为我看到一些人建议使用它,但它似乎不起作用。有些人遇到了控制器尚未初始化的问题,但我在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;
});
}
}]);
您不能广播$rootScope事件处理程序。你最多只能发射$
$rootScope.$emit('loggedIn');
根据文件
$emit(name, args);
通过作用域向上分派事件名称层次结构通知注册的CCD_ 2侦听器。事件生命周期从调用
$emit
的作用域开始。所有侦听此作用域上的name事件的侦听器都会收到通知。然后,事件向上遍历到根作用域沿途呼叫所有注册的侦听器。活动将停止如果某个侦听器取消它,则进行传播。