2015-08-15 40 views
1

我沒有HTML輸出,但當我console.log我的$http.get的結果我有我想要的對象。有人可以解釋我如何從我的模板中獲取$http.get的數據嗎?在angularjs中解析沒有數據(ui.router)

.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; 
     }); 
    } 

}); 

回答

2

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; 
    }); 
} 
+0

謝謝!有效 – kper

1

你需要從UserService.all方法返回的承諾(return $http.get(...)):

function all() { 
    return $http.get('/api/contacts').then(function(payload) { 
     return payload.data; 
    }); 
}