Search code examples
angularjsmeanjsangular-resourceangularjs-ng-resource

chaining query with $resource


I have the following service which uses ngResource to set my syllableCount be clicking a button.

 //Words service used to communicate Words REST endpoints
(function () {
  'use strict';

  angular
    .module('words')
    .factory('querywordsService', querywordsService);

  querywordsService.$inject = ['$resource'];

  function querywordsService($resource) {
    return $resource('api/words/?syllableCount=:count', {syllableCount: '@count'},
    {
      update: {
        method: 'PUT'
      }
    });
  }
})();

If the syllableCount button 2 is clicked the query shows the specific word. This is how it looks in the Controller:

    $scope.searchCount = function (count) {
      $scope.searchWords = querywordsService.query({count: count});
};

Everything works fine! But i need more than syllableCount inside the query. There are at least 4 other parameters that should go inside. If i just chain them like:

api/words/?syllableCount=:count&?syllableStructur=:struct .... 

its not working.

Is there a good way to chain multiple queries like above?


Solution

  • It works if i use it like this:

        //Words service used to communicate Words REST endpoints
    (function () {
      'use strict';
    
      angular
      .module('words')
      .factory('querywordsService', querywordsService);
    
      querywordsService.$inject = ['$resource'];
    
      function querywordsService($resource) {
    
        var wordsData = $resource('/api/words/?syllableCount=:count&syllableStructur=:struct&frequency=:freq&emphasis=:emph',
        { count: '@count', struct: '@struct', freq: '@freq', emph: '@emph' },
          {
            update: {
              method: 'PUT'
            }
          });
    
          return {
            wordsData: wordsData
          };
      }
    })();
    

    in the controller i set the different parameters to the value i need with ng-click from the buttons.