0

i have query regarding passing param objects to $resource get method.

in the backend restful application i am not getting the routeparam values passed . here my sample code

on clicking the search button from the html page it is moving to controller as mentioned in myapp.js

myapp.js var app = angular.module('myApp', [ 'MyServices']);

app.config([ '$routeProvider', function($routeProvider) {

$routeProvider  
.when('/employeesearch/:empID/:deptID', {
    templateUrl : 'partials/result.html',
    controller : SearchController
})
.otherwise({
    redirectTo : '/defaultsearch'
});} ]);

on doing console.log in controller.js file , the routeparams values are displayed correctly

SearchController.js

function SearchController($scope, $routeParams, EmployeeSearch) $scope.mydata = EmployeeSearch.getSearchResult($routeParams.empID,$routeParams.deptID);

}

myservices.js

angular.module('MyServices', ['ngResource']).

factory('EmployeeSearch', function($resource){
return $resource('rest/employees/:eID/:dID', {}, {
query: {method:'GET', params:{}, isArray:false},
getSearchResult:{method:'GET', params:{eID:'empID',dID:'deptID'}}
});
});

backend restful java class

@Path("/employees") public class EmployeeSearchService {

@Path("{eID}/{dID}")

@GET

@Produces(MediaType.APPLICATION_JSON)

public Employee searchmethod(@PathParam("eID") String empId, @PathParam("dID") String deptId) {

     System.out.println("eid:"+empId);
     System.out.println("did:"+deptId);
        return new Employee(); }

on hitting the restful url :http:localhost:9080/MyProject/rest/employees/e12/d12
- the value of eid is 'e12' and did is "d12"

but on hitting via the anugular it is displaying the value of eid as "empID" and did as "deptID",

it is displaying the value as i mentioned in the myservices.js file

Can you please help me out in this? what am i giving wrongly?

Reference site: https://github.com/teunh/jfall2012/blob/master/demo/web/index.html

4

1 回答 1

1

paramsgetSearchResult用于默认值,因此当您在调用(empIDdeptID)时未提供它们时,它们会被发送到服务器

factory('EmployeeSearch', function($resource){
    return $resource('rest/employees/:eID/:dID', {}, {
        query: {method:'GET', params:{}, isArray:false},
        getSearchResult:{method:'GET', params:{eID:'empID',dID:'deptID'}}
    });
});

要传递您在 中捕获的值$routeParams,您可以将调用中的对象中的值传递给getSearchResult控制器​​,如下所示:

function SearchController($scope, $routeParams, EmployeeSearch) {
    $scope.mydata = EmployeeSearch.getSearchResult({
        eID: $routeParams.empID,
        dID: $routeParams.deptID
    });
}

请注意,对象的键必须与您的资源路径参数的名称(:eID:dID)匹配

于 2013-07-02T12:58:57.557 回答