1

passing parameters to $resource in angularjs.

I have a search button in the UI upon clicking which I want to pass the input parameter to the $resource which would call the REST service in the backend.

when I pass a string value in the .query it works perfectly fine, but the @issueinput is not getting resolved to the input value.

this is the app.js

var myApp = angular.module('myApp',['ui.router', 'ngResource']);

myApp.factory('SearchService',function($resource){
    return $resource("http://localhost:8081/support/api/search/:issuename");
});

this is the controller

angular
  .module('myApp')
  .controller('searchCtrl',['$scope', '$window','SearchService', function($scope, $window, SearchService){

     $scope.data={};

    $scope.search = function(issueinput){    
     SearchService.query({issuename:'@issueinput'},function(response){
            $scope.data.issues = response; 
         });
    };
  }  
  ]);

this is the UI html

<html>
<div id="searchpage">
     <label>Search</label> :<input data-ng-model="issueinput"></input> {{issueinput}}  
     <br/>
     <br/>
     <br/>
     <button data-ng-click="search(issueinput)">Search</button>
     <br/>
     <br/>
<ul>
  <li data-ng-repeat="s in data.issues">{{s.issueName}} : {{s.issueDescription}}</li>
</ul>


</div>
</html>

1 Answer 1

3

I got it to work by making the following changes

controller

$scope.search = function(){  
SearchService.get({issuename: $scope.issueinput},function(response){
$scope.data.issues = response; 
});

$resource in app.js

myApp.factory('SearchService',['$resource',function($resource){
    return $resource("http://localhost:8081/support/api/issue/:issuename",{},
       {
        get:{
             method: 'GET',
             isArray:true}}
    );
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

big help, was about pulling my hair out and found this post. what did it for me was adding the parameter on this line SearchService.get({issuename: $scope.issueinput},function(response){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.