Getting $scope object in Angular's run() method

前端 未结 2 749
难免孤独
难免孤独 2020-12-28 12:50

I\'d like to do some stuff when my app loads to set up the default state. So I\'m trying to use the run method on the Module object. When I try to access the $scope variable

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-28 13:32

    app.run(function ($rootScope) {
        $rootScope.someData = {message: "hello"};
    });
    

    You can only get $rootScope injected to services and run function, because each child scope is inherited from its parent scope and the top level scope is rootScope. Since it would be ambigous to inject any scope. Only root scope is provided.

提交回复
热议问题