1
我正在研究angularJS,但我仍然是初学者...而我有一个简单的问题,我希望你能回答。使用location.pathAngularJS等待数据呈现视图?
app.config(function($routeProvider) {
$routeProvider
.when('/', {
controller:'ListCtrl',
templateUrl:'list.html'
})
.when('/update/:itemId', {
controller:'UpdateCtrl',
templateUrl:'update.html'
})
[...]
.otherwise({
redirectTo:'/'
});
});
从“名单”的观点,我重新根“更新”的观点:
app.controller('ListCtrl', function($scope, albumFactory, $location, $http) {
$scope.albums = albumFactory.getList().then(function(albums){
$scope.albums = albums;
});
[...]
$scope.updateAlbum = function(index) {
console.log('updateAlbum()');
$location.path("/update/" + $scope.albums.albums[index].id);
}
在更新
我得到了以下路由控制器我需要首先检索细节以预先填充视图。为此,我使用的工厂如下:
app.controller('UpdateCtrl', function($scope, albumFactory, $location, $routeParams, $http) {
$scope.album = albumFactory.get($routeParams.itemId).then(function(album){
$scope.album = album;
});
所以我的问题是视图首先呈现(显示)为空。一旦来自工厂的Ajax调用完成,作用域就会更新并且视图被填充。
是否可以在渲染局部视图之前等待工厂回复? 或者我做错了什么?
的目的是为了避免在很短的时间,其中的观点是空的......(不是真正用户友好的)
请参阅'resolve:'[docs](http://docs.angularjs.org/api/ngRoute.$routeProvider) – calebboyd