简体   繁体   中英

How to call a factory which reads a json file, inside a .run method in angular js?Is it possible or not?

I need to call a factory inside run method.Is it possible or not? And the factory call returns a array of objects. My code is :

 myApp.run(function($rootScope, $location, $route, AuthService) {
    $rootScope.$on('$routeChangeStart',
        function(event, next, current) {
            $rootScope.menuPermission = next.access.restricted
            /***Need to call a factory here***/
            AuthService.getUserStatus()
                .then(function() {
                    if (next.access.restricted && !AuthService.isLoggedIn()) {
                        $location.path('/login');
                        $route.reload();
                    }
                });
           });
   });

Yes. You can call a factory inside your run method.

Factory :

myApp.factory('YourFactory', function(){
   var svc={};
   svc.getData = function(){
     //your processing logic
   } 
   return svc;
 });

Run method :

myApp.run(function($rootScope, $location, $route, AuthService, YourFactory) {
$rootScope.$on('$routeChangeStart',
function(event, next, current) {
    $rootScope.menuPermission = next.access.restricted
    YourFactory.getData().then(function(){
     // do what you want here
    });
    AuthService.getUserStatus()
        .then(function() {
            if (next.access.restricted && !AuthService.isLoggedIn())
         {
                $location.path('/login');
                $route.reload();
            }
        });
});

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