在Angular应用程序的主页上,我有一个视图和一个嵌套视图(请参阅下面的ui-router代码).对于视图,我对我的数据库进行$http调用并使用ng-repeat显示结果.同时我将这些结果推送到服务中.在嵌套视图中,我从服务中调用一个结果并显示它(我使用两个控制器).我的问题是我需要在加载时发生所有这些. ui-router中的“解决方案”是解决此问题的最佳方法吗?
$urlRouterProvider.otherwise('/open'); $urlRouterProvider.when('/open','/open/mainbook'); $stateProvider .state('open',{ url: '/open',templateUrl: 'ang/views/open.html',}) .state('open.mainbook',{ url: '/{featId}',templateUrl: 'ang/views/open.mainBook.html',})`
解决方法
解决方案是不使用服务,将两个控制器合并为一个,并使用$state.go在初始$http调用内立即调用嵌套视图(我也在同一个调用中给出嵌套视图数据),即
$scope.featCall = function(){
$HTTP({
方法:’GET’,
缓存:’true’,
url:’/ featList’
}).then(function(result){
$scope.single = [];
$scope.single = result.data [0];
$state.go(‘open.mainbook’,{
featId:$scope.single.id
});
bookData.clearProduct();
angular.forEach(result.data,function(eachOne){
$scope.featList.push(eachOne);
bookData.addProduct(eachOne);
})
})
};
$scope.featCall = function(){
$HTTP({
方法:’GET’,
缓存:’true’,
url:’/ featList’
}).then(function(result){
$scope.single = [];
$scope.single = result.data [0];
$state.go(‘open.mainbook’,{
featId:$scope.single.id
});
bookData.clearProduct();
angular.forEach(result.data,function(eachOne){
$scope.featList.push(eachOne);
bookData.addProduct(eachOne);
})
})
};
请注意,我仍然使用服务(bookData)来更改嵌套视图的后续更改,而不是用于其数据的初始填充.