Angularjs $ resource url路径

时间:2015-07-01 05:28:22

标签: angularjs service resteasy

这是我第一次尝试使用参数将方法传递给url并遇到一些问题。基本上,我通过控制器和服务将字符串值从html页面传递给服务器(我正在使用restEasy框架),但我一直收到404未找到的错误。

所以代码如下。

调用控制器的html代码段:

<div class="container">
<a href="#notificationGroup" class="btn btn-success" role="button">Create New Distribution List</a>
<div class="panel panel-primary center-block" style="margin-bottom: 10px;margin-top: 10px">
    <div class="panel-heading">
    </div>
    <div id="searchfield">
        <form id="searchFieldForm" autocomplete="on">
            <h4>Code: <input type="text" data-ng-model="code" class="biginput" name="code" >
                Name: <input type="text" name="name" data-ng-model="name"  class="biginput">
                <button data-ng-click="search(name, code)" type="button" class="btn btn-primary">
                    Search</button>
            </h4>
        </form>
    </div>
</div>

控制器:

myApp.controller('SearchController',
function($scope,SearchCollection,$location) {
    $scope.search = function(name, code) {
        $scope.distributionLists = SearchResultCollection.search(name, code);
        $location.path('/list');
    }
});

服务:

myApp.factory('SearchResultCollection', function($resource) {
return $resource('../api/foo/:name/SearchSingle/:code',
    {name:'@name', code:'@code'}, {
        search : {method: 'GET',
            isArray: true}
    })
});

我得到的错误是:

GET http://localhost:9080/api/foo/SearchSingle 404 (Not Found)

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

试试这段代码:

myApp.controller('SearchController',
        function($scope, SearchCollection, $location) {
            $scope.search = function(name, code) {
                $scope.distributionLists = SearchResultCollection.search({name: name, code: code});
                $location.path('/list');
            };
        });

myApp.factory('SearchResultCollection', function($resource) {
    return $resource('../api/foo/:name/SearchSingle/:code', {name: '@name', code: '@code'}, {
        search: {method: 'GET', isArray: true}
    });
}); 

希望它有所帮助。