2017-06-06 63 views
0

我需要在運行方法中調用工廠。是否有可能? 工廠調用返回一個對象數組。 我的代碼是:如何在angular js中的.run方法中調用讀取json文件的工廠?是否可能?

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(); 
        } 
       }); 
     }); 
}); 
+0

絕對有可能..會很容易幫助.. – tanmay

回答

2

是的。您可以在運行方法中調用工廠。

廠:

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

Run方法:如果你的國家,你所面對的確切問題/錯誤

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(); 
      } 
     }); 
}); 
相關問題