我正在尝试在用户登录后关闭一个功能。我尝试将广播包装在一个计时器中,因为我看到有人建议它,但它似乎没有起作用。有些人遇到的问题是他们的控制器还没有初始化,但我在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;
});
}
}]);
答案 0 :(得分:2)
您无法广播(或旁边)$ rootScope事件处理程序。你只能以$排放它。
$rootScope.$emit('loggedIn');
$emit(name, args);
通过范围向上调度事件名称 层次结构通知已注册的$rootScope.Scope
侦听器。事件生命周期从调用
$emit
的范围开始。 监听此范围内的名称事件的所有侦听器都会收到通知。 之后,事件向上遍历根范围并且 一路上调用所有注册的听众。活动将停止 如果其中一个听众取消它,则传播。