Answer the question
In order to leave comments, you need to log in
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
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));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question