Q:No data in template when resolving in angularjs (ui.router) |
Q:没有数据模板解决AngularJS(UI。路由器) |
I've got no HTML output but when I console.log the result of my $http.get I've got the object I want to have. Can someone explain me how to get the data from $http.get in my template?
.state('secure/contacts',{
url:'/secure/contacts',
template:"Contacts: {{contacts | json}}",
resolve:{
contacts : function(UserService){
return UserService.all();
}
},
controller:function($scope,contacts){
$scope.contacts = contacts;
}
})
.service('UserService',function($http){
return {
get:get,
all:all
} ;
function all(){
$http.get('/api/contacts').then(function(payload){
console.log(payload.data);
return payload.data;
});
}
function get(id){
return $http.get('/api/user/'+id).then(function(payload){
return payload.data;
});
}
});
|
我没有HTML输出,但当我console.log结果我得到了我想要的对象,我http.get美元。有人可以解释我如何为我的模板http.get获取数据?
.state('secure/contacts',{
url:'/secure/contacts',
template:"Contacts: {{contacts | json}}",
resolve:{
contacts : function(UserService){
return UserService.all();
}
},
controller:function($scope,contacts){
$scope.contacts = contacts;
}
})
.service('UserService',function($http){
return {
get:get,
all:all
} ;
function all(){
$http.get('/api/contacts').then(function(payload){
console.log(payload.data);
return payload.data;
});
}
function get(id){
return $http.get('/api/user/'+id).then(function(payload){
return payload.data;
});
}
});
|
answer1: |
回答1: |
Your all() function doesn't return anything.
Replace
function all(){
$http.get('/api/contacts').then(function(payload){
console.log(payload.data);
return payload.data;
});
}
by
function all(){
return $http.get('/api/contacts').then(function(payload){
console.log(payload.data);
return payload.data;
});
}
|
你的all()函数不返回任何东西。
更换
function all(){
$http.get('/api/contacts').then(function(payload){
console.log(payload.data);
return payload.data;
});
}
通过
function all(){
return $http.get('/api/contacts').then(function(payload){
console.log(payload.data);
return payload.data;
});
}
|
answer2: |
回答2: |
You need to return promise (return $http.get(...)) from UserService.all method:
function all() {
return $http.get('/api/contacts').then(function(payload) {
return payload.data;
});
}
|
你需要返回的承诺(返回的HTTP。得到(…))从userservice.all方法:
function all() {
return $http.get('/api/contacts').then(function(payload) {
return payload.data;
});
}
|
javascript
angularjs
|
|