$resource, Data.query({other_id: 1}) 正在检索所有数据
$resource, Data.query({other_id: 1}) is retrieving all the data
我有资料在休息api:
{
'id': 1,
'name': 'test',
'other_id': 1
},
{
'id': 2,
'name': 'test2',
'other_id': 1
},
{
'id': 3,
'name': 'test3',
'other_id': 2
}
我尝试使用 Data.query({other_id: 1}) 和 Data.query({'other_id': 1}) 但两者都检索了 3 行,我想要第一行和第二行
我做错了什么?
您将希望使用 $resource 获取方法而不是查询。查询将 return 一个集合,而获取 return 个单个元素。
来自 docs 的示例:
var User = $resource('/user/:userId', {userId:'@id'});
var user = User.get({userId:123});
编辑
查询不能 return 集合的子集,它 return 是您端点提供的整个集合。为了获得集合中对象的子集,您有两个选择:
修改您的后端端点以期望 ID 列表,并且只有 return 具有匹配 ID 的对象。
var data = Data.query({ids:'1,2'});
获取所有对象,简单过滤掉不相关的。
var desiredObjects = [1,2];
Data.query().then(function(data) {
data.filter(function(a) {
return desiredObjects.indexOf(a.id) > -1
});
});
我有资料在休息api:
{
'id': 1,
'name': 'test',
'other_id': 1
},
{
'id': 2,
'name': 'test2',
'other_id': 1
},
{
'id': 3,
'name': 'test3',
'other_id': 2
}
我尝试使用 Data.query({other_id: 1}) 和 Data.query({'other_id': 1}) 但两者都检索了 3 行,我想要第一行和第二行
我做错了什么?
您将希望使用 $resource 获取方法而不是查询。查询将 return 一个集合,而获取 return 个单个元素。
来自 docs 的示例:
var User = $resource('/user/:userId', {userId:'@id'});
var user = User.get({userId:123});
编辑
查询不能 return 集合的子集,它 return 是您端点提供的整个集合。为了获得集合中对象的子集,您有两个选择:
修改您的后端端点以期望 ID 列表,并且只有 return 具有匹配 ID 的对象。
var data = Data.query({ids:'1,2'});
获取所有对象,简单过滤掉不相关的。
var desiredObjects = [1,2]; Data.query().then(function(data) { data.filter(function(a) { return desiredObjects.indexOf(a.id) > -1 }); });