[英]Resolve in Angular UI Router
在我的角度采樣路線中,我正在嘗試做:
(function(angular){
'use strict';
angular
.module('appRouter',['ui.router'])
.controller('routerCtrl',function(myResolveServ,resolveData){
console.log(resolveData);
})
.service('myResolveServ',function($http){
this.getResolveData = function($http){
var root = 'https://jsonplaceholder.typicode.com';
return $http.get(root+'/posts/1').then(function(response){
return response.data;
});
}
})
.config(function($stateProvider,$urlRouterProvider){
$stateProvider
.state('settings.profile',{
url:'/profile',
templateUrl:'templates/profile.html'
})
.state('settings.account',{
url:'/account',
templateUrl:'templates/account.html',
controller:'routerCtrl',
controllerAs:'vm',
resolve:{
resolveData :function(myResolveServ){
var data = myResolveServ.getResolveData();
}
}
});
$urlRouterProvider.otherwise('/settings/profile');
});
})(window.angular);
但是此代碼不起作用。 我無法導航到“帳戶頁面URL” /settings/account
。
錯誤在控制台中不明顯。
我究竟做錯了什么 ?
UPDATE
我知道這可行:
resolve:{
resolveData : function($http){
var root = 'https://jsonplaceholder.typicode.com';
return $http.get(root+'/posts/1').then(function(response){
return response.data;
});
}
}
您需要在解析中返回數據以進行路線settings.account
resolveData: function(myResolveServ) {
return myResolveServ.getResolveData();
}
很難說為什么,您應該注意ui路由器錯誤。 粘貼下面的代碼,查找錯誤
var $rootScope = angular.element(document.querySelectorAll("#home")[0]).injector().get('$rootScope');
$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
console.log('$stateChangeStart to '+toState.to+'- fired when the transition begins. toState,toParams : \n',toState, toParams);
});
$rootScope.$on('$stateChangeError',function(event, toState, toParams, fromState, fromParams){
console.log('$stateChangeError - fired when an error occurs during transition.');
console.log(arguments);
});
$rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){
console.log('$stateChangeSuccess to '+toState.name+'- fired once the state transition is complete.');
});
$rootScope.$on('$viewContentLoaded',function(event){
console.log('$viewContentLoaded - fired after dom rendered',event);
});
$rootScope.$on('$stateNotFound',function(event, unfoundState, fromState, fromParams){
console.log('$stateNotFound '+unfoundState.to+' - fired when a state cannot be found by its name.');
console.log(unfoundState, fromState, fromParams);
});`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.