Excuse me, I made a page in which the header part is imported from ng include (there is a logincontroller in it to determine whether to display the login button). At the same time, I defined a login page in ngroute, which also uses logincontroller. I passed $http After getting the data, I call back to the scope, but the view page is not updated. It seems that the same controller has two scopes. Why is this? (I found that when nginclude is completed, the things in the logincontroller will be executed once, and will be triggered again when entering the login page)
I also tried $apply() as mentioned online and the result will be an error
function($scope,$http,$location,$timeout,userService,notifyService) {
$scope.login = {
userid:"bbbbb",
}
$scope.user={islogged:0}
$timeout(function(){
//$scope.user={islogged:1}
console.log($scope.user);
},5000)
$scope.httplogin = function(){
$http({
method:'POST',
url:server.domain+server.login,
data:{
userid:$scope.login.userid,
password:$scope.login.password,
},
}).success(function(d){
if(d.code == 200){
$scope.login={userid:1111111}
$scope.user={islogged:1}
notifyService.make('success',d.data.msg,5)
}else{
alert(d.msg)
}
})
}
The above is the code of the controller, which contains the consolelog in the timeout. If it is printed, three will appear at once. Is it because I use the same ngcontroller in three places? If the comment call $scope.user={islogged:1} in timeout will be printed like this, which is abnormal for my task. I hope they are all the same
Object {islogged: 0}
Object {islogged: 0}
Object {islogged: 1}
If you uncomment $scope.user={islogged:1} and let it run, the three controllers will be printed normally.
Object {islogged: 1}
Object {islogged: 1}
Object {islogged: 1}
================
I just tested it again, and it only modifies the value under the currently triggered ngcontroller. If there are multiple ngcontrollers, the values of other ngcontrollers will not change, whether it is under nginclude or ngview. How to synchronize data modified by the same ngcontroller to another place?
Are you sure it’s the same scope?
ng-include
A new scope will be created.After reading the code
It is true that each controller is a separate instance and does not affect each other. You can consider putting the data status under a global (higher level) scope; a better way would be to manage it uniformly through service
For example:
$scope.$apply() doesn’t work?
It’s best to make sure that what you see
$scope
in two places is the same scope, rather than one being a subscope of the other or something.It would be better to post the code~
I also encountered the same problem. When $scope was changed for the first time, the data on the interface did not move. But when the interface changed (for example, when it started to fade out) the data would change immediately, and then $scope would change again. , the interface changes accordingly.
I also tried using $apply, but it would report a long list of errors. It is said that something is already in use, etc... I don't understand... I hope there is a solution.