Question

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

Was it helpful?

Solution

params is for default values thus they are sent to the server when you didn't provide them in your call to getSearchResult (empID and deptID)

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

To pass the values you captured in $routeParams, you can pass the values in an object in the call to getSearchResult in your controller as follow:

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

Note that the key of the object has to match the name of your resource path parameter (:eID and :dID)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top