ng-resource查询返回所有记录

时间:2015-06-05 18:44:41

标签: javascript angularjs angularjs-ng-repeat odata

我有像这样的资源设置

(function () {
'use strict';
var storeCommandServiceRoot = '/api/StoreCommandsRest/:id';

angular.module('common.service')
.factory("storeCommandResource", ["$resource", "appSettings", storeCommandResource])
function storeCommandResource($resource, appSettings) {
    return $resource(appSettings.serverPath + storeCommandServiceRoot, { id: '@id' },
           { "update": { method: "PUT" } });
}}());

我试图通过查询名为“Serial”的属性来获取记录,这是一个整数。

        function getStoreCommandsBySerial() {
        var storeCommandGet = storeCommandResource.get({ Serial: 3 });
        storeCommandGet.$promise.then(function (response) {
            debugger;
        });
    }

但作为回应,我得到了所有记录。我期待只有Seri​​al = 3的记录。 发送的网址类似于

http://localhost:xxxx/api/StoreCommandsRest?Serial=3

在服务器端,它是一个具有基于REST功能的oDataController。

1 个答案:

答案 0 :(得分:0)

好像你的参数名称不一致。在工厂中,您有{ id: '@id' },但在控制器中,您使用:storeCommandResource.get({ Serial: 3 });

尝试将storeCommandResource.get({ Serial: 3 });更改为storeCommandResource.get({id: 3 }); 另外,你的storeCommandServiceRoot应该是'/ api / StoreCommandsRest /?id =:id'

如果您打算在网址中保留“Serial”,请确保将其余内容更改为“Serial”而不是在ID和Serial之间混合