0

I have problem with my first AngularJS app. I'm using yeoman generator. I need to get data from json file and check the length of array with objects.

My data.json file:

{"persons":[
    {  
         "firstname":"Christian",
         "lastname":"Bower",
         "age":21
      },
      {  
         "firstname":"Anthony",
         "lastname":"Martial",
         "age":25
      }
]}

My angular controller:

   'use strict';

        /**
         * @ngdoc function
         * @name webdevApp.controller:DataTableCtrl
         * @description
         * # DataTableCtrl
         * Controller of the webdevApp
         */
        angular.module('webdevApp')
          .controller('DataTableCtrl', function ($scope, $http) {
            this.awesomeThings = [
              'HTML5 Boilerplate',
              'AngularJS',
              'Karma'
            ];


            $http.get('../data.json').success(function(data){
                $scope.Data = data;
                $scope.namePerson = $scope.Data.persons[0].firstname;
            });

         console.log($scope.namePerson);
         console.log($scope.Data.length);
      });

Console output:

1) undefined

2) TypeError: Cannot read property 'length' of undefined at new

Next question is - How can I achieve effect as below (array with objects)?

$scope.persons = [
    { 
        "firstname": "christian", 
        "lastname": "bower", 
        "age": 21
    },
    { 
        "firstname": "anthony1", 
        "lastname": "martial", 
        "age": 25 
    }];

2 Answers 2

2

$http.get is an asyncronous call so you have to output its results in the success function like this:

       $http.get('../data.json').success(function(data){
            $scope.Data = data;
            $scope.namePerson = $scope.Data.persons[0].firstname;

            console.log($scope.namePerson);
            console.log($scope.Data.length);

            $scope.persons = data.persons; // get your persons array
        });
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! I have to do all operations inside .success function ? How can I use my variables outside ? .hasOwnProperty method will be the simplest way to count my array items?
@XingD123, yes, whatever you do to your asynchronous call results - you do it in the success handler (meaning after the data is loaded). To get the number of array items use the length propery.
Ok, but if I need to do something like that $scope.getData = funcion () { $http.get().... } and later call my getData() function ? Is there another method to reach variables in whole script ?
Ok I have my answer. I'll use $.ajax() asynchronous.
0

The $http.get call is asynchronous!

Put the logs in your success function.

$http.get('../data.json').success(function(data){
    $scope.Data = data;
    $scope.namePerson = $scope.Data.persons[0].firstname;

    //assign persons in scope
    $scope.persons = data.persons;

    //logs
    console.log($scope.namePerson);
    console.log($scope.Data.length);
});

Note as a side remark that IMHO you should only assign data or even personsin scope since you can get everything from there. Putting thousands of useless variables in scope can create unwanted complexity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.