Answer the question
In order to leave comments, you need to log in
How to get data from the received dictionary (json)?
I'm trying to link angularjs and django rest framework. But I don’t know how to get the data if I get it through the service. But the data comes, it is visible in the console.
Here is my code for angular:
var app = angular.module('example.app.basic', ['videoServices',]);
app.controller('AppController', ['$scope', 'Results',
function($scope, Results) {
$scope.page = 1;
$scope.results = Results.query();
$scope.videos = $scope.results['results']; //$scope.results.item пробовал
}
]);
var videoServices = angular.module('videoServices', ['ngResource']);
videoServices.factory('Results', ['$resource',
function($resource){
return $resource('/api/videos/:videoId?format=json&page=:page', {}, {
query: {method:'GET', params:{videoId:'', page:'1'}}
});
}]);
{
"count": 21975,
"next": "http://127.0.0.1:8000/api/videos/?format=json&page=2",
"previous": null,
"results": [
{
"id": 22416,
"video_id": "9RAvpnWvynk",
"title": "sdsdsd",
"description": ""
},
{
"id": 22415,
"video_id": "ohgUhgf-dk",
"title": "sdsdsd",
"description": ""
}
]
}
(function() {
var app;
app = angular.module('example.app.basic', ['videoServices']);
app.controller('AppController', [
'$scope', '$http', function($scope, $http) {
$scope.videos = [];
$scope.page = 1;
$scope.next = function() {
return $http.get('/api/videos/?format=json&page=' + $scope.page).then(function (result) {
$scope.page += 1;
return angular.forEach(result.data['results'], function (item) {
return $scope.videos.push(item);
});
});
};
return $scope.next();
}
]);
}).call(this);
Answer the question
In order to leave comments, you need to log in
Yes, there is not so much to learn, json is very simple.
$scope.results['results'] // здесь лежит не один элемент, а весь массив
$scope.results['results'][0] // здесь лежит первый по порядку элемент
$scope.results['results'][1] // здесь лежит второй по порядку элемент
$scope.videos = $scope.results['results']; //$scope.results.item triedthe request has not yet been completed. I did with $http (I used doc.jsfiddle.net/use/echo.html to simulate the request)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question