我已经配置了resolve
返回 Promise 的多个路由的参数,以便延迟控制器的实例化,直到 Promise 得到解决。目前我使用函数符号,而不是指定要注入的字符串。
例如:
.when('/article/:id', {
templateUrl: 'app/article/article.html',
controller: 'ArticleController',
resolve: {
article: ['Content', '$route', function (Content, $route) {
return Content.get({ contentId: $route.current.params.id }).$promise;
}]
}
})
The article
参数已正确注入已解析的承诺值。
但是,我想保持它的干燥并通过指定一个字符串来注入这个值,如下所示文档中提到 http://docs.angularjs.org/api/ngRoute.%24routeProvider.
当我设置一个工厂函数来提供像这样的承诺时,实例仅正确注入一次(第一次)。此后,使用相同的 Promise,因为 AngularJS 注入服务已缓存该实例。
.factory('contentPromise', ['Content', '$route', function (Content, $route) {
return Content.get({ contentId: $route.current.params.id }).$promise;
}])
是否可以指定每次请求时都必须运行工厂函数?或者以其他方式实现我的目标?