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;
});
}
}]);
我实际上尝试使用发射第一,这似乎并没有工作。 – Rich
@然后,您需要创建一个小型重击器或小提琴来重现问题。在AngularJS中发生事件有一些非常严格和快速的规则,所以如果这不能解决你的问题,你的代码中很可能会有另一个问题,在你的问题中不明显。 –