Note that there are some explanatory texts on larger screens.

plurals
  1. POAngular resource call not fired when called from $scope.$watch
    primarykey
    data
    text
    <p>I created a directive that watches for a <code>day</code> attribute and retrieves a remote resource whenever the attribute value changes. CoffeeScript code :</p> <pre><code>angular.module('app.directives').directive 'myDirective', ['$timeout', ($timeout)-&gt; DirectiveController=($scope, activityResource)-&gt; # Load activities load= ()-&gt; activityResource.get { day: $scope.day environment_id: $scope.environment.id }, (data)-&gt; $scope.activities = data.activities # Watch environment and reload activities $scope.$watch 'environment', (value) -&gt; load() # Watch selected day to reload activities $scope.$watch 'day', (value) -&gt; load() ... ] </code></pre> <p>The <code>load</code> method is called each time the <code>day</code> changes but the result callback never triggers. Network inspection shows that the request is not sent to the remote end. The only workaround I found is to defer the execution of the <code>load</code> method using the <code>$timeout</code> service :</p> <pre><code> $scope.$watch 'day', (value) -&gt; $timeout (()-&gt; load()), 10 </code></pre> <p>I suspect an issue related to the scope lifecycle but I could not figure it out why I had to defer the call to the resource.</p> <p><strong>Update</strong> :</p> <p>The resource source code :</p> <pre><code>resources = angular.module('app.resources', ['ngResource']) ... resources.factory 'app.activityResource', ['$resource', 'app.endpoint', ($resource, endpoint)-&gt; $resource "#{endpoint()}/user/environments/:environment_id/activities/:id/:verb", {environment_id: "@environment_id", id: "@id"}, latest: method : 'GET' params: verb: 'latest' annual: method : 'GET' params: verb: 'annual' ] </code></pre> <p>I also added to the code excerpt the second watched attribute (<code>environment</code>) used as a parameter to query the resource.</p> <p><strong>Update 2</strong></p> <p>I don't know if it is related but we're using CORS to access the remote end (which seems to work well).</p>
    singulars
    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.
 

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