How to access/update $rootScope from outside Angular

后端 未结 3 1044
半阙折子戏
半阙折子戏 2020-11-29 04:13

My application initializes an object graph in $rootScope, like this ...

var myApp = angular.module(\'myApp\', []);

myApp.run(function ($rootScope) {
    $ro         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 04:35

    If you want to update root scope's object, inject $rootScope into your controller:

    myApp.controller('MyController', function ($scope, $timeout, $rootScope) {
    
        $rootScope.myObject = { value: 3 };
    
        $timeout(function() {
    
            $rootScope.myObject = { value: 4 };
    
            $timeout(function () {
                $rootScope.myObject = { value: 5 };
            }, 1000);
    
        }, 1000);
    });
    

    Demo fiddle

提交回复
热议问题