0
我有一個$scope
變量isAuth
這是一個boolean
,當用戶登錄時這是真實的。我使用這個變量來顯示使用ng-hide的頁腳。我也有一個$routeProvider
服務的路由定義。該html代碼有maincontroller
和另一個控制器測試控制的子集。在用戶使用服務的login()
功能登錄後,我調用位置服務將用戶路由到由不同控制器控制的「測試」路徑 - 測試。雖然'測試'只控制HTML文檔的<ng-view>
部分,但文檔的其餘部分仍然使用maincontroller
。 我遇到的問題是即使$isAuth
的值在servicecall()
函數中得到更新,但它並不反映在html中。
角碼
ub.config(function($routeProvider){
$routeProvider
.when('/',{
templateUrl: '../pages/home.htm',
controller:'mainController'
})
.when('/test',{
templateUrl: '../pages/test.htm',
controller:'test'
})
var ub = angular.module('ub',['ngRoute']);
ub.controller('mainCtrl',['$scope','testsrv',function($scope,testsrv){
$scope.isAuth = false;
$scope.msg="";
$scope.servicecall = function(){
console.log('in service call function');
testsrv.login().then(function(response){
$scope.msg="in service call function";
$scope.isAuth = true;
$location.path('/test');
});
};//servicecall
}]);
ub.service('testsrv',['$q','$log',function($q,$log){
var self=this;
self.login = function(){
var def = $q.defer();
var msg='calling a service function';
$log.log(msg);
def.resolve(msg);
return def.promise;
};
}]);
HTML
<body ng-app='ub'>
<div ng-controller="mainCtrl">
<button ng-click="servicecall()">click me!</button>
<ng-view>
</ng-view>
<br>
{{isAuth}}
<br>
{{msg}}
</div>
</body>
分配變量$ rootScope服務解決了這個問題,你能做到一個$ scope。$在login()中應用?例如: 'testsrv.login()。then(function(response){ $ scope.msg =「in service call function」; $ scope.isAuth = true; $ scope。$ apply() $ location.path('/ test'); });' – Toddsden
ya,試過已經得到'$ digest已在進行中'的錯誤。看起來這不是問題 –
@georgeawg,也沒有解決問題 –