admin管理员组文章数量:1332395
I have configured the resolve
parameter of several routes to return a promise in order to delay instantiation of the controller until the promise is resolved. Currently I am using the function notation, rather than specifying a string to inject.
For example:
.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
parameter is correctly injected with the resolved promise value.
However, I would like to keep it DRY and inject this value by specifying a string, as is mentioned in the documentation.
When I set up a factory function to provide the promise like so, the instance is only injected correctly once (the first time). Thereafter, the same promise is used because the AngularJS injection service has cached the instance.
.factory('contentPromise', ['Content', '$route', function (Content, $route) {
return Content.get({ contentId: $route.current.params.id }).$promise;
}])
Is it possible to specify that the factory function must be run every time it is requested? Or to achieve my goal in some other way?
I have configured the resolve
parameter of several routes to return a promise in order to delay instantiation of the controller until the promise is resolved. Currently I am using the function notation, rather than specifying a string to inject.
For example:
.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
parameter is correctly injected with the resolved promise value.
However, I would like to keep it DRY and inject this value by specifying a string, as is mentioned in the documentation.
When I set up a factory function to provide the promise like so, the instance is only injected correctly once (the first time). Thereafter, the same promise is used because the AngularJS injection service has cached the instance.
.factory('contentPromise', ['Content', '$route', function (Content, $route) {
return Content.get({ contentId: $route.current.params.id }).$promise;
}])
Is it possible to specify that the factory function must be run every time it is requested? Or to achieve my goal in some other way?
Share Improve this question asked Feb 12, 2014 at 17:30 AlexAlex 7,9193 gold badges47 silver badges61 bronze badges 3- The first example you have is the way to go. – Ye Liu Commented Feb 12, 2014 at 17:50
- Hi, I'm trying to do the same thing. Where/how did you define Content? I know you're using ngResource. Is Content defined in .config? – user636044 Commented May 21, 2014 at 16:15
-
It's registered as a factory, returning
$resource('api/Content/:id')
– Alex Commented May 22, 2014 at 10:02
1 Answer
Reset to default 7The first example you have is the way to go, I think you could go a little bit "DRYer" like this:
.when('/article/:id', {
...
resolve: {
article: ['contentPromise', function (contentPromise) {
return contentPromise();
}]
}
})
Service:
.factory('contentPromise', ['Content', '$route', function (Content, $route) {
return function() {
return Content.get({ contentId: $route.current.params.id }).$promise;
};
}])
本文标签: javascriptUsing the ngRoute 39resolve39 parameter with an injected promiseStack Overflow
版权声明:本文标题:javascript - Using the ngRoute 'resolve' parameter with an injected promise - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742283318a2446496.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论