Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS - Using $resource.query with params object
    primarykey
    data
    text
    <p>I'm trying to pick up <a href="http://angularjs.org" rel="noreferrer"><code>angular.js</code></a> and working on figuring out some of the things that are a bit less documented.</p> <p>Consider this - I have a search method on the server that accepts query params and returns a collection of search results, and responds to <code>GET /search.json</code> route (Rails FWIW).</p> <p>So with <code>jQuery</code>, a sample query would look like this:</p> <pre><code>$.getJSON('/search', { q: "javascript", limit: 10 }, function(resp) { // resp is an array of objects: [ { ... }, { ... }, ... ] }); </code></pre> <p>I'm trying implement this using angular, and wrap my head around how it works. This is what I have now:</p> <pre><code>var app = angular.module('searchApp', ['ngResource']); app.controller('SearchController', ['$scope', '$resource', function($scope, $resource){ $scope.search = function() { var Search = $resource('/search.json'); Search.query({ q: "javascript", limit: 10 }, function(resp){ // I expected resp be the same as before, i.e // an array of Resource objects: [ { ... }, { ... }, ... ] }); } }]); </code></pre> <p>And in the view:</p> <pre><code>&lt;body ng-app="searchApp"&gt; ... &lt;div ng-controller="SearchController"&gt; ... &lt;form ng-submit="search()"&gt;...&lt;/form&gt; ... &lt;/div&gt; &lt;/body&gt; </code></pre> <p>However, I keep getting errors like <code>TypeError: Object #&lt;Resource&gt; has no method 'push'</code> and <code>$apply already in progress</code>. </p> <p>Things seem to work out as expected if I change the <code>$resource</code> initialization to the following:</p> <pre><code>var Search = $resource("/search.json?" + $.param({ q: "javascript", limit: 10 })); Search.query(function(resp){ ... }); </code></pre> <p>It seems more intuitive to initialize the <code>$resource</code> once and then pass different query parameters with changes in the requested search. I wonder if I'm doing it wrong (most likely) or just misunderstood the docs that calling <code>$resource.query</code> with the query params object as the first argument is feasible. thanks. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload