Angularjs 在决心中注入决心
我想知道,是否可以将一个resolve参数注入另一个resolve参数。代码将告诉您更多信息:Angularjs 在决心中注入决心,angularjs,angular-ui-router,resolve,Angularjs,Angular Ui Router,Resolve,我想知道,是否可以将一个resolve参数注入另一个resolve参数。代码将告诉您更多信息: .state('videos.videosLection', { url : '/:lection', templateUrl : '/partials/videosLection.html', controller : 'VideosLectionCtrl', menuItem : 'Videos', resolve :
.state('videos.videosLection', {
url : '/:lection',
templateUrl : '/partials/videosLection.html',
controller : 'VideosLectionCtrl',
menuItem : 'Videos',
resolve : {
selectedLection : function ($stateParams, lections) {
...
},
sections : function(Section, selectedLection) {
...
}
}
})
我使用angular UI路由器进行路由。我需要在开始解析节之前进行SelectedSelection。有什么想法吗?非常感谢您能将
SelectedSelection
片段转化为服务吗?然后就这样做:
resolve: {
sections: function(Section, selectedLectionService, $stateParams, lections){
var result = selectedLectionService($stateParams, lections);
// do something with result here...
}
}
我不知道这是否能满足你的所有需求,但也许它能帮你找到正确的方向。如果没有效果,请告诉我。我终于明白了,这里的方法是只解析一个对象,它有两个属性,它们本身就是承诺。查看代码:
resolve : {
data : function($stateParams, lections, Video, Section, $q) {
// defer for the data object
var defer = $q.defer();
// selectedLection
var selectedLection = null;
// if there is lection selected
if($stateParams.lection)
{
// lection is selected
lections.forEach( function(lection)
{
// find the selected lection
if(lection.addressUrl == $stateParams.lection)
{
// save it
selectedLection = lection;
}
});
}
else
{
// resolve it with everything empty
defer.resolve({
selectedLection : null,
lectionVideos : [],
sections: []
});
return defer.promise;
}
// promise to the lection videos
var deflectionvideos = $q.defer();
// find all the videos by the lection id
Video.GetAllByLection(selectedLection.id, null, function(data) {
// resolve it with the data
deflectionvideos.resolve(data);
}, function(error) {
// resolve it with empty array
deflectionvideos.resolve([]);
});
// promise for the sections
var defsections = $q.defer();
// find all the sections of selected lectino
Section.GetAllByLection(selectedLection.id, function(data) {
// resolve it with the data
defsections.resolve(data);
}, function(error) {
// resolve it with empty array
defsections.resolve([]);
});
// wait, untill all the promises for sections and lectionsvideos are resolved
$q.all([
deflectionvideos.promise,
defsections.promise
]).then(function(data) {
// resolve it with actual data
defer.resolve({
selectedLection : selectedLection,
lectionVideos : data[0],
sections: data[1]
});
});
// return promise for the data object
return defer.promise;
}
}
如果你们有其他方法或更好的解决方案,请告诉我:-)
希望它能帮助别人不。解析继承仅适用于从父状态到子状态。问题是,由于某种原因,我无法访问服务中的$stateParams,因此此解决方案对我不起作用。。或者我做错了什么:-)