Global variables in AngularJS

前端 未结 12 1933
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 12:05

I have a problem where i\'m initialising a variable on the scope in a controller. Then it gets changed in another controller when a user logs in. This variable is used to co

12条回答
  •  一个人的身影
    2020-11-22 12:26

    You can also use the environment variable $window so that a global variable declare outside a controller can be checked inside a $watch

    var initWatch = function($scope,$window){
        $scope.$watch(function(scope) { return $window.globalVar },
            function(newValue) {
                $scope.updateDisplayedVar(newValue);
        });
    }
    

    Becareful, the digest cycle is longer with these global values, so it is not always real-timed updated. I need to investigate on that digest time with this configuration.

提交回复
热议问题