V
V
Vlad1712015-05-08 12:15:57
JavaScript
Vlad171, 2015-05-08 12:15:57

How to get data from $resource object?

There is an API with which I work through $resource. Through var model=Mymodel.query() I get an object of the form [$promise: Promise, $resolved: false], in which an array with data sits in $promise. Yes, I understand, asynchrony, receiving data through promises, etc. On a theoretical level, it's understandable. But there is a task - to get data of type model.data from Mymodel.query (for example) and save it to a variable. Attempts like:
model.$promise.then(function(response) {
$scope.data=response.data;
})
return undefined. Any connoisseurs? Explain to the amateur what the truth is ......

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Dunayevsky, 2015-05-08
@Vlad171

It is correct to write like this:

(function(A) {
    "use strict";
    A.module('App').controller('Ctrl', ['$resource', '$scope', functino($resource, $scope) {
        var r = $resource('/api/items/:id/', {
            id: '@id'
        }, {
            update: {
                method: 'PUT' //Необязательная фича для Django REST Framework
            }
        });
        $scope.loading = true;
        $scope.items = [];

        r.query({
            //А можно передать параметры, скажем, limit и offset
        }, function(response) {
            $scope.items = response;
            $scope.loading = false;
        }, function(response) {
            $scope.loadign = false;
            $scope.errors = response.data; // Так DRF отдаёт ошибки
        });
    }]);
}(this.angular));

M
Mikhail Osher, 2015-05-08
@miraage

ngResource - not really.
Restangular - fire.

S
Sergey Gavrilov, 2015-05-08
@FireVolkhov

And if you look in the debugger, what is in 'response'?

V
Vlad171, 2015-05-08
@Vlad171

Weird. It seems that Angular is fashionable now, but there are few real experts on it...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question