0
我有以下代碼:
angular.module('DemoApp')
.controller('NavbarCtrl', ['$scope', '$rootScope', '$location', '$timeout', function($scope, $rootScope, $location, $timeout) {
$scope.logout = function() {
$rootScope.user = null;
$location.path("/login");
};
}]);
的問題是,當註銷被調用時,位置不會改變。但是,下面的代碼工程(注意使用$超時):
angular.module('DemoApp')
.controller('NavbarCtrl', ['$scope', '$rootScope', '$location', '$timeout', function($scope, $rootScope, $location, $timeout) {
$scope.logout = function() {
$rootScope.user = null;
$timeout(function() {
$location.path("/login");
});
};
}]);
有人可以告訴我爲什麼這是嗎?
編輯: 的HTML看起來像這樣,是NavbarCtrl控制器的元素:
<a href="#" ng-click="logout()"><i class="fa fa-sign-out"></i><br/>Abmelden</a>
$ timeout沒有延遲正在使用作爲hack它觸發$摘要循環 – Whisher
^同意。我會試着在$ location.path(「/ login」)調用後觸發$ scope($ apply())的摘要循環。 –
範圍將結果應用於表明摘要已在進行中的例外 –