我在resolve函数中有很多相同的代码。是否有可能使其具有可重复使用的功能并将其传递给决心?
FROM THIS...
.when('/workers', {
resolve: {
"check": function () {
if (!isLoggedIn() || !isAdmin()) {
window.location.href = '#/about';
}
},
},
templateUrl: 'html/admin/workers.html',
controller: 'adminWorkersController'
})
TO SOMETHING LIKE THIS:
.when('/workers', {
resolve: myResolveFunction()
templateUrl: 'html/admin/workers.html',
controller: 'adminWorkersController'
})
答案 0 :(得分:1)
您可以将服务用于可重用代码。例如:
resolve: {
"check": function(yourService) {//inject service
yourService.method(); //this method of the service contains reusable code
}
}
答案 1 :(得分:1)
您可以为路线解析创建提供商
var app = angular.module('app', []);
//Must be a provider since it will be injected into module.config()
app.provider('routeResolver', function () {
this.$get = function () {
return this;
};
this.route = function () {
var resolve = function () {
// resolve
}
return {resolve: resolve};
};
});
app.config( function(routeResolverProvider) {
.when('/workers', {
resolve: routeResolverProvider.resolve()
templateUrl: 'html/admin/workers.html',
controller: 'adminWorkersController'
})
})