![](/img/trans.png)
[英]Angular UI-Router cannot resolve child state (with dynamic URL param) from its parent state
[英]UI-Router: Can I resolve a parent promise in a child state?
我正在研究社交應用程序,該應用程序在個人資料頁面中有封面。 “編輯”和“訪問”頁面都有封面,但有不同的小節。 問題在於封面中顯示的某些控制器嚴格取決於您當前正在訪問的部分。
為了解決此問題,我試圖在父狀態的“ resolve”部分中返回一個promise,然后在不同的子狀態中解決promise。
.state('profile', {
url: '/profile',
controller: 'ProfileController',
templateUrl: 'components/profile/profile.html',
resolve: {
actor: function(UserService){
return UserService.getUser();
},
target: function($q){
var deferred = $q.defer();
return deferred.promise;
}
}
})
.state('profile.view', {
url: '/:userId',
templateUrl: 'components/profile/view.html',
navbar: true,
parent: 'profile',
resolve: {
/**
*
* @param target
* @param {UserService} UserService
*/
target: function(target, UserService){
target.resolve(UserService.getActions('view'));
}
}
})
不幸的是,我的邏輯有問題,因為配置文件狀態內的return deferred.promise
阻止了應用程序。
有人可以幫我弄清楚我在做什么錯嗎?
最好!
將promise 返回到解析器功能
.state('profile.view', {
url: '/:userId',
templateUrl: 'components/profile/view.html',
navbar: true,
parent: 'profile',
resolve: {
/**
*
* @param target
* @param {UserService} UserService
*/
target: function(target, UserService){
return UserService.getActions('view').$promise;
}
}
})
該答案假定UserService
是$resource
查詢。 其他API返回的諾言有所不同,但原理相同。 將promise 返回到解析程序功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.