angular.js - The problem of $scope not refreshing in angular
ringa_lee
ringa_lee 2017-05-15 16:51:08
0
5
1035

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?

ringa_lee
ringa_lee

ringa_lee

reply all(5)
世界只因有你

Are you sure it’s the same scope? ng-includeA 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:

html<body ng-app="app">

    <p ng-controller="loginCtrl">
        <pre>{{info}}</pre>
    </p>
    <script type="text/javascript">
    var app = angular.module('app', []);

    app.factory('login', ['$timeout', function($timeout){
        var ret = {

            info: {
                isIn: false
            }
        };

        // 模拟ajax请求
        $timeout(function() {
            ret.info.isIn = true;
        }, 1000);

        return ret;
    }]);

    app.controller('loginCtrl', ['$scope', 'login', function($scope, login){

        $scope.info = login.info;
    }])
    </script>
</body>
刘奇

$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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template