我需要在 run 方法中调用工厂。是否可能? 工厂调用返回一个对象数组。 我的代码是:
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();
}
});
});
});
最佳答案
是的。您可以在 run 方法中调用工厂。
工厂:
myApp.factory('YourFactory', function(){
var svc={};
svc.getData = function(){
//your processing logic
}
return svc;
});
运行方法:
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();
}
});
});
关于javascript - 如何在 Angular js 中的 .run 方法内调用读取 json 文件的工厂?是否可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44387700/