I
I
Ilya2014-11-19 23:45:15
JavaScript
Ilya, 2014-11-19 23:45:15

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'}}
    });
  }]);

And in json I have this:
{
    "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": ""
        }
    ]
}

Probably send JS to learn? You will be right. But I don't want to know (I don't want to yet).
And this is how it works:
(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);

If suddenly you came from a search engine, and you don’t know how to display at least somehow, then just inside the template, refer to the data through a dot. In my case ng-repeat="video in videos.results"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Benkovsky, 2014-11-20
@FireGM

Yes, there is not so much to learn, json is very simple.

$scope.results['results'] // здесь лежит не один элемент, а весь массив
$scope.results['results'][0] // здесь лежит первый по порядку элемент
$scope.results['results'][1] // здесь лежит второй по порядку элемент

if you need to find an element by id, then you have to go through the entire array and search with the desired id
UPD: jsfiddle.net/m27ow8rz/22
$resource is very cumbersome, if you want to figure it out for yourself, query definitely implies that you will get back an array, not an object. Plus, $resource is a promise and needs to be used via callback, by the time you try
$scope.videos = $scope.results['results']; //$scope.results.item tried
the 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 question

Ask a Question

731 491 924 answers to any question