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,也没有解决问题 –