Saeed Baba
Saeed Baba

Reputation: 188

passing parameters to resource factory in angularjs from controller to search in database

Explanation

I have a textbox in view that choose which model to be displayed. I need to pass a variable from view to the angular resource factory, to be passed to the Server Controller to decide which model should be passed to the Client Controller.

As you see I pass a variable called issuename to resource and then I expect to choose the model in controller by the magnitude of this parameter.

I type Jeans in the textbox in view which means that issuename should be equal to Jeans and it should give me the SAME result as when I assign the value of searchParam to Jeans manually.

BUT it seems like it is not passed properly because when I comment out that line in Server Controller I cannot see Jeans model anymore.

I have tried:

  1. req.body.issuename
  2. req.params.issuename
  3. req.issuename

And still it is not working So there should be another problem

Thanks in advance for your help!

Code Section

Client Service ( $resource )

angular.module('alls').factory('Alls', ['$resource', function($resource) {
return $resource('alls/:issuename', {},
    {
      get:{ method: 'GET', isArray:true }
    });
}]);

Client Controller

angular.module('alls').controller('AllsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Alls', 'Jeans',
function($scope, $stateParams, $location, Authentication, Alls, Jeans) {

      $scope.search = function(){
            Alls.get({issuename: $scope.issueinput},function(response){
                $scope.alls = response;
            });
     }; 
};

Client View

<section data-ng-controller="AllsController">
<div class="page-header"> 
    <h1>Alls</h1></div>
    <div class="Search">
        <h2>Search Section</h2>
        <label>Search</label> :  <input data-ng-model="issueinput"></input> {{issueinput}}
        <button data-ng-click="search()">Search</button>
    </div>

    <br></br><br></br>
    <h2>Result Section</h2>

    <div class="list-group">
        <table class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Color</th>
                </tr>
            </thead>
            <tbody>
                <tr data-ng-repeat="all in alls">
                    <td data-ng-bind="all.name"></td>
                    <td data-ng-bind="all.color"></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

Server Controller

exports.list = function(req, res) {

    var searchParam = req.body.issuename;

    searchParam = 'Jeans';  // Comment Out or Not //

    mongoose.model(searchParam).find().sort('-created').populate('user', 'displayName').exec(function(err, alls) {
        res.jsonp(alls);
    });
};

Upvotes: 1

Views: 742

Answers (1)

Kremnev Sergey
Kremnev Sergey

Reputation: 332

I think you need to pass the "issuename" parameter in your $resource declaration via "@"-notation. Like so:

angular.module('alls').factory('Alls', ['$resource', function($resource) {
return $resource('alls/:issuename', {issuename: '@issuename'},
    {
      get:{ method: 'GET', isArray:true }
    });
}]);

This will tell your resource to take the property "issuename" from the object being posted when you call Alls.get({issuename: $scope.issueinput}

You can read more about that in the $resource docs

Upvotes: 1

Related Questions