4

I have this factory:

factory('getJson', ['$resource', function($resource) {
    return $resource('json/greetings.json', {}, {
      query: {method:'GET', isArray:true}
    });
  }]);

the file is hardcoded, 'greetings.json', and I want the factory to get files according to the checkboxes in my view:

<li><input type="checkbox" ng-model="includeVeggies" />Veggies</li>
<li><input type="checkbox" ng-model="includeGreetings" />Greetings</li>

Any idea how do I go about that?

1 Answer 1

7

You can return a function:

.factory('getJson', ['$resource', function($resource) {
    return function (file) {
        return $resource(file, {}, {
            query: {method:'GET', isArray:true}
        });
    };
}]);

Then in your controller:

.controller('MyController', ['getJson', function (getJson) {
    getJson('json/greetings.json');
});

Here's a Plnkr.

Sign up to request clarification or add additional context in comments.

4 Comments

it returns undefined.
Also, in this way, how do I make a callback to assign the response only after the server has fetched the file?
Sorry, there was a typo.
Got it! I was missing getJson('json/greetings.json').query()

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.