AngularJS: resolve to return value of $resouce

297 views Asked by At

When I get the set point which is written on a JSON file using a $resource, how is it settled? $promise of $resource don't have resolve function.

myApp.factory('prop', function($resource) {
  return {
    getSetting: function(key) {
      var res = $resource('setting.json').query();  // *1

      /* ? What is the processing here? */

      return res[key];
    }
  }
});

update add write

res is resource object. res have $promise. At the time *1, res is not settled. I want to be settled in this function. What kind of processing should I add?


Is it like that?

myApp.factory('prop', function($resource) {
  return {
    getSetting: function(key) {
      return $resource('setting.json').query().$promise.then(function(res) {
        return res[key];
      }
    }
  }
});
1

There are 1 answers

1
Freezystem On

$resource return a resource object containing an attribute $promise. You can then on it.

var res = $resource('setting.json').$promise
    .then(
        function(result){//success handling},
        function(error){//error handling}
    );