2017-09-11 4 views
0

누군가 내가 실수를 저지르고 있음을 지적 할 수 있습니까?각도를 사용하여 특정 Json 데이터를 추출하십시오.

그것과 라인을 통해 이루어집니다 JSON 오브젝트의 배열은 "이름"필드를 인쇄하기위한 것입니다 매우 간단한 응용 프로그램 :

{{ctrl.contact[0].results[0].name.first}} 또는 {{ctrl.contact[1].results[0].name.first}}


(이 자체가 내가 각 JSON 블록 개별적으로 루프에 의해 여기의 이름을 인쇄 할 수 없습니다

) 매우 복잡한 것 같습니다 것은 내가 시도 것입니다 :

 <div ng-repeat="i in ctrl.contact"> 
      <span>{{ctrl.contact[i].results[0].name.first}}</span> 
     </div> 

내 각도 설정 (앱, 컨트롤러 등)을 조정하고 편집하는 데 몇 시간을 보낸 후 자신감이 좋습니다. 아래

코드 :

<html ng-app="ContactAppApp"> 
 
<head> 
 
    <title>My Contact App</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script> 
 
</head> 
 

 
<body> 
 
    <div> 
 
     <div ng-controller="ContactAppController as ctrl"> 
 
       
 
      <h1>{{ctrl.test}}</h1> 
 
      
 
      <div ng-repeat="i in ctrl.contact"> 
 
       <span>{{ctrl.contact[0].results[0].name.first}}</span> 
 
      </div> 
 
      
 
     </div> 
 
     
 
    </div> 
 
</body> 
 

 
<script> 
 
    
 
    var app = angular.module("ContactAppApp", []) 
 
    app.controller("ContactAppController", ContactAppController); 
 
    
 
    function ContactAppController() { 
 
     
 
     this.test = "This text is generated by Angular"; 
 
     
 
     this.contact = [ 
 
      { 
 
       "results": [ 
 
        { 
 
         "gender": "male", 
 
         "name": { 
 
          "title": "mr", 
 
          "first": "tony", 
 
          "last": "cruz" 
 
         }, 
 
         "location": { 
 
          "street": "9813 north road", 
 
          "city": "edinburgh", 
 
          "state": "humberside", 
 
          "postcode": "E84 4YD" 
 
         } 
 
        } 
 
       ] 
 
      }, 
 
      { 
 
       "results": [ 
 
        { 
 
         "gender": "male", 
 
         "name": { 
 
          "title": "mr", 
 
          "first": "Jack", 
 
          "last": "cruz" 
 
         }, 
 
         "location": { 
 
          "street": "9813 north road", 
 
          "city": "edinburgh", 
 
          "state": "humberside", 
 
          "postcode": "E84 4YD" 
 
         } 
 
        } 
 
       ] 
 
      } 
 
     ] 
 
    } 
 

 
</script> 
 
</html>

답변

1

은 다음을 시도해보십시오

<div ng-repeat="i in ctrl.contact"> 
    <span>{{i.results[0].name.first}}</span> 
</div> 
+0

좋습니다. 일해 줘서 고마워. 그럼 내가 뭘 말하는거야? 그것은 현재 반복에서 보유하고있는 객체입니까? 그것은 doesnt 루프 카운터/응답을 기반으로 숫자가 될 것 같습니다 – Kevin

+0

'i'는 현재 개체를 말합니다. ctrl.contact "문법에서'ng-repeat ="(i, contact)를 사용할 수 있습니다.이 경우'i'는 현재 색인을 참조 할 것입니다. 또한 특별한 키워드'$ index'를 ['ngRepeat'] (https://docs.angularjs.org/api/ng/directive/ngRepeat) 지시어와 함께 사용할 수 있습니다. –

+0

올바른 경우,이 경우'i'는 배열'ctrl.contact'의 각 요소를 나타냅니다. –

0

나는 조금 다르게 배열을 설정합니다. 당신이 배열의 항목, 사용 $ 지수의 인덱스를 추적하려는 경우,

<div ng-repeat="item in contact.results"> 
    <span>{{item.name.first}} {{$index}}</span> 
</div> 

하지 : 이런 식으로 뭔가를 시도, 당신의 NG 반복에서 다음

this.contact = { 
    "results": [ 
      { 
      "gender": "male", 
      "name": { 
       "title": "mr", 
       "first": "tony", 
       "last": "cruz" 
      }, 
      "location": { 
       "street": "9813 north road", 
       "city": "edinburgh", 
       "state": "humberside", 
       "postcode": "E84 4YD" 
      } 
     }, 
     { 
       "gender": "male", 
       "name": { 
        "title": "mr", 
        "first": "Jack", 
        "last": "cruz" 
       }, 
       "location": { 
        "street": "9813 north road", 
        "city": "edinburgh", 
        "state": "humberside", 
        "postcode": "E84 4YD" 
       } 
     } 
    ] 
} 

이 같은 시도 나는.

관련 문제