Angularjs $stateProvider中的服务
我试图使用Angularjs $stateProvider中的服务,angularjs,Angularjs,我试图使用resolve并将其注入工厂,然后在控制器中打印解析返回的数据 基本上,代码看起来很像: angular .module('appRouter',['ui.router']) .controller('routerCtrl',function(resolveData){ console.log(resolveData); }) .factory('repoService',function($h
resolve
并将其注入工厂
,然后在控制器
中打印解析返回的数据
基本上,代码看起来很像:
angular
.module('appRouter',['ui.router'])
.controller('routerCtrl',function(resolveData){
console.log(resolveData);
})
.factory('repoService',function($http){
var API = "https://jsonplaceholder.typicode.com/posts/1";
return {
getResolveData : function($http){
return $http.get(API);
}
}
})
.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(repoService){
return repoService.getResolveData().then(function(response){
return response.data;
});
}
}
});
$urlRouterProvider.otherwise('/settings/profile');
});
但代码不起作用<代码>控制台中无错误,但路由到设置/帐户不起作用。控制台上没有打印任何内容。
但是
如果我们不注入服务,下面的代码就可以很好地工作
.state('settings.account',{
url:'/account',
templateUrl:'templates/account.html',
controller:'routerCtrl',
controllerAs:'vm',
resolve:{
resolveData : function(){
var root = 'https://jsonplaceholder.typicode.com';
return $http.get(root+'/posts/1').then(function(response){
return response.data;
});
}
}
});
我不知道我做错了什么?请解释。
getResolveData:function($http){
remove$http它已经在类级别上定义了。@Walfrat可爱。谢谢。