2014-06-25 37 views
0

我在angularjs中出現了一個奇怪的錯誤。我使用$資源模塊中angularjs在剩下的服務器端我有一個的find動作返回一個JSON數組上面的網址搜索方法註冊該服務

$provide.service("CustomerService", ['$resource', function ($resource) { 
    return $resource('/com/caspco/customers/:url:id', {}, { 
     query: { method: 'GET', isArray: true, params: {id: '@id'}}, 
     find: { method: 'POST', isArray: true ,params:{url:'search'}}, 
     .......... other actions .......... 
    }); 
}]); 

,使其餘的請求。當我通過這種方式在控制器所謂find行動:

service.$find().$promise.$then(function (res) { 
    console.log("resource is" + res); 
}, function (error) { 
    console.log("error"); 
}); 

它提出了對服務

TypeError: (anonymous function) angular.js:2070 
(anonymous function) angular.js:1516 
k.$apply angular.js:2575 
(anonymous function) angular.js:4323 
o.event.dispatch jquery.js:3 
r.handle 
+0

有關錯誤的更多詳細信息... – gkalpak

+0

有沒有包含angular-resources JS文件? – Eylen

+0

@Eylen是的!我已經包含了 – Pooya

回答

0

方法不是由「$」字符作爲前綴。有關從服務返回資源的方法前綴爲'$'。 $ promise屬性上的'then'方法也不以'$'爲前綴。

我想如果你清理語法,你將會走在正確的軌道上。

the angular docs.

+0

服務器的響應發生問題 – Pooya

+0

並且僅在服務響應數組響應時發生 – Pooya

0

首先你的代碼來調用服務應該是:

service.find().$promise.then(function (res) { 
    console.log("resource is" + res); 
}, function (error) { 
    console.log("error"); 
}); 

接下來的事情來檢查,其實是你的反應陣列。我被這個問題困擾了很長時間,我正在調用一個服務來獲取後端對象的列表,但在返回它們之前,後端將它們包裝在一個Response對象中,所以接收到的角度不是數組,它是裏面有一個數組的對象。

相關問題