2013-10-25 80 views
3

我正在嘗試使用$ resource進行ajax調用,並在從服務器接收數據時加載數據表。但是當我調用get()時,我得到$ promise爲未定義的。 我正在使用工廠撥打電話。 這裏是我廠:

app.factory('getAllUsers', [ '$resource', function($resource) { 
return $resource('URL', {}, {get : {method : 'GET'}}); 
}]); 

而且控制器:

 app.controller('MyController',function($scope,$location,getAllUsers){ 

     console.log("In controller"); 

     getAllUsers.get().$promise.then(function(data) { 

      loadDatatable(data); 
      }); 


    }); 

ERROR:getAllUsers.get()$承諾是不確定的

注:我在我的應用程序包括ngResource和index.html中的angular-resource.js也是如此。

回答

0

由於您使用$resource你可以做

getAllUsers.get({},function(data) { 
    loadDatatable(data); 
}); 
0

你忘記添加.json作爲調用類型嗎?

app.factory "User", ["$resource", ($resource) -> 
    $resource("https://stackoverflow.com/users/:id.json", {id: "@id"}, {update: {method: "PUT"}}) 
]