kev*_*ius 8 angularjs ngresource
我正在学习angular的$ resource服务,在角度教程中添加了一个自定义操作(查询),其方法设置为'get',isArray设置为true
return $resource('phones/:phoneId.json', {}, {
query: {method:'GET', params:{phoneId:'phones'}, isArray:true}
});
Run Code Online (Sandbox Code Playgroud)
但是,如果查看$ resource的文档, 'query'操作已将其方法设置为'get',并且默认情况下isArray已设置为true .所以我想我可以把这些房产留下来.
这适用于方法属性,但事实证明,如果我省略了isArray属性,我会收到此错误:
错误:[$ resource:badcfg]操作的资源配置出错
query.包含一个对象但得到一个数组的预期响应
这是为什么?
Way*_*ery 16
我认为你误解了文档.
默认情况下,不添加任何自定义操作,支持以下操作:
'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
Run Code Online (Sandbox Code Playgroud)
因此,默认情况下,query操作需要返回一个有意义的数组,因为查询通常会返回一个项目数组.
所以如果你使用:
phonecatServices.factory('Phone', ['$resource', function($resource){
return $resource('phones/phones.json');
}]);
Run Code Online (Sandbox Code Playgroud)
然后,您可以执行如下查询:
var queryParams = { name: 'test' };
Phone.query(queryParams, {}, function (response) {
$scope.phones = response;
});
Run Code Online (Sandbox Code Playgroud)
现在,如果您想添加自定义操作,则默认isArray值为false:
return $resource('phones/:phoneId.json', {}, {
someCustomAction: {method:'GET', params:{phoneId:'phones'} }
});
Run Code Online (Sandbox Code Playgroud)
需要返回一个对象.如果返回了一个数组,则isArray需要将其设置为true如此:
return $resource('phones/:phoneId.json', {}, {
someCustomAction: {method:'GET', params:{phoneId:'phones'}, isArray: true }
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10865 次 |
| 最近记录: |