【发布时间】:2014-04-08 08:40:19
【问题描述】:
您好,我想在加载任何控制器之前从工厂加载数据。我找到了一种使用解析的方法:
angular.module('agent', ['ngRoute','ui.bootstrap','general_module', 'order_module','customer_module']).config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/customer', {
templateUrl: 'customer_module/partials/customer.php',
resolve:{
'MyServiceData':function(loginvalidation){
// MyServiceData will also be injectable in your controller, if you don't want this you could create a new promise with the $q service
var a=loginvalidation.check_usertype();
}}
}
);
$routeProvider.otherwise({redirectTo: '/customer'});
}]);
但问题是我必须在每条路线上都复制它。 angularjs有没有办法在启动任何控制器之前编写一次代码以加载服务(不复制我的代码)?
谢谢
【问题讨论】:
-
简单方法:只需将您的解析主体分解为某个方法,然后为每个路由设置 resolve: myResolve()。见:stackoverflow.com/questions/11972026/…
标签: javascript angularjs angularjs-directive angularjs-service angularjs-routing