简体   繁体   中英

From run block can i able to call angular scope function inside another controller instead of $rootscope

I need to call angular Scope function inside some controller from runblock. can i able to call the function or else need to use rootScope or $scope.apply.

First method:

myAPP.run(function ($rootScope, $state,$scope) {

  $scope.menupage();

});

 var SampleController = function ($scope,$localStorage) {

$scope.menupage =function()
{
// some logic
};
});

    SampleController.$inject = ['$scope','$localStorage'];

Else need to use like this

myAPP.run(function ($rootScope, $state,$scope) {

 $scope.$apply(function() {
  $scope.menupage();

});

});



var SampleController = function ($scope,$localStorage) {


$scope.menupage =function()
{


// some logic
};


});
SampleController.$inject = ['$scope','$localStorage'];
myAPP.run(function ($rootScope, $state,$scope) {

You can't have $scope object as a third parameter here, because both $scope and $controller objects are created during compiling and linking phase , which follows run phase during which run blocks are executed. So you can't have them in your run blocks. Only the $rootScope is available in run blocks since it's a service and only services are available in run blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM