1

Here's example setup:

var editor = angular.module("editor",['ngResource']);
editor.factory("Provider",function($resource){
    return $resource('/api/test/1').get();
})

function ctrl($scope,Provider){
    $scope.sections = Provider; 
}

My problem is that in firebug i see that there are two GET requests being sent: one at /api/test/1 and one at /api/test/1/.

Any ideas what might be causing this?

4
  • Are you creating multiple instances of the ctrl? Also you have a syntax error missing a " in your code. If you have multiple controllers of this type being used then for each one the function will be called which in this case is calling get() on the $resource. If you want a single instance use service instead of factory. Commented Jul 22, 2013 at 20:11
  • No I'm using "ctrl" only once, just tried with service but it was the same. Commented Jul 22, 2013 at 20:27
  • 1
    Can you make a jsfiddle out of this or a plunkr to show the issue in action? I've only been using $http within my services thus far but haven't seen any double call behavior... though I've mostly been testing in Chrome. Commented Jul 22, 2013 at 20:29
  • Actually $http did the trick, Thank you! Commented Jul 22, 2013 at 20:42

2 Answers 2

1

I found that just adding /# after /1 solved the problem.

Change:

return $resource('/api/test/1')....

to:

return $resource('/api/test/1/#')....
Sign up to request clarification or add additional context in comments.

Comments

1

For sake of completion, I believe $http will 'short circuit' within your folder structure, simply grabbing the resource where you point it to do so and is accessible within the project, whereas $resource is geared towards communicating with an external source, often an external RESTful api. In applications where I implement this, I often define a variable where my various $resource definitions can access it, as you'll see in my defined variable API

It also appears in this case that you are passing 1 as some sort of id.

var API = 'http://localhost\\:#####/api/';

editor.factory('Provider', function ($resource) {
    return $resource(API + 'test/:id', { id: '@id' }, { });
});

Then to implement

function ctrl($scope, Provider){
    var myVarToPass = 1;

    Provider.get({ id: myVarToPass }, function (result) {
        //single call here as soon as controller loads
    });
}

Comments

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.