D
D
daniilkholodniy2015-04-07 14:34:00
Angular
daniilkholodniy, 2015-04-07 14:34:00

How to save query result when using $resource?

Why when using code like:

$http.get(url).
    success(function(response) {
      $scope.receiveData = response;
   });

the receiveData variable stores the data received upon request,
and in the case of $resource :
var someServices = angular.module('someServices', ['ngResource']);

    someServices.factory('QueryesService', ['$resource', function($resource) {
      return $resource('http://url/:currentService/:param');
    }]);

    var someControllers = angular.module('someControllers', []);

    someControllers.controller('someCtrl', ['$scope', '$http', 'QueryesService', function($scope, $http, QueryesService) {

      $scope.getSomeData = function() {
        QueryesService.get({currentService : 'service1'}, function(response) {
          $scope.receiveData = response;
        });
      };
                        $scope.receiveData; //undefined
               }]);

when accessing the receiveData variable, undefined is returned ? I know that it's a matter of asynchrony and deffered promise , but on my own I just can't figure out how to still save the result of the request so that this variable can be used as in the case of $http.get.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yuri, 2015-04-07
@Gilga

MB$scope.receiveData = response.data;

R
Ramallah, 2015-04-07
@ramallah

Because promises and asynchrony.

S
Sergey Gavrilov, 2015-04-17
@FireVolkhov

Maybe

.controller 'someCtrl', ($scope, QueryesService) ->
  $scope.receiveData = {}
  $scope.getSomeData = ->
    QueryesService.get currentService : 'service1', (response) ->
       angular.extend $scope.receiveData, response

It's not clear from the text what exactly you want to do.
Maybe IcedCoffeeScript will help you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question