2017-03-26 5 views
-2

データベースからのクエリ結果を持つjsonオブジェクトを正常に返しました。返されたjsonに1つずつアクセスできません。angularjs配列のjsonデータにアクセスできません

はここconsole.logを示して何: enter image description here

角度コード:私はconsole.log($scope.peopleList[0]);を使用する場合

$scope.peopleList=[]; 
      $http.get("http://localhost/project/public/people_names").then(function(response) { 
       $scope.peopleList.push(response.data); 
      }); 
      console.log($scope.peopleList); 

が、それはundefinedを言います。どのように私はそれがコードの下ng-repeat

+1

。 – Hosar

+1

代わりに、応答オブジェクトのdata propをpeopleListに割り当てるだけで済みます。 'ng-repeat'で簡単にループすることができます – Umair

+0

[非同期呼び出しからの応答を返すにはどうすればいいですか?](http://stackoverflow.com/questions/14220321/how-do-i-return非同期呼び出しからの応答) – georgeawg

答えて

1

てみ使用してそれを印刷するためにアクセスします:いくつかの観測

$scope.peopleList=[]; 
$http.get("http://localhost/project/public/people_names") 
    .then(function(response) { 
    $scope.peopleList = response.data; 
    console.log($scope.peopleList); 
    }); 

そしてng-repeat

<div ng-repeat='person in peopleList track by person.id'> 
    .... 
</div> 
0

で以下のように使用する:

  • をとして、 response.dataはalreaですdyはarray of objectsです。したがって、別の配列に再度プッシュする必要はありません。
  • あなたはそれがあなたに期待される結果を提供しますconsole.log($scope.peopleList[0])使用することができ、配列から最初のオブジェクトにアクセスしようとすると、直接、今$scope.peopleList = response.data代わりの$scope.peopleList.push(response.data)

を使用することができます。 **そして、あなたは非同期呼び出しを行っているので、console.log` `と試みるのですが、内部**

DEMO

var myApp = angular.module('myApp',[]); 
 

 
myApp.controller('MyCtrl', function($scope) { 
 
    $scope.peopleList = [{ 
 
     id: 5, 
 
     name:"Raman" 
 
    }, 
 
    { 
 
     id: 7, 
 
     name:"Komal" 
 
    }]; 
 
    console.log($scope.peopleList[0]); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyCtrl"> 
 
</div>

関連する問題