누군가 내가 실수를 저지르고 있음을 지적 할 수 있습니까?각도를 사용하여 특정 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>
좋습니다. 일해 줘서 고마워. 그럼 내가 뭘 말하는거야? 그것은 현재 반복에서 보유하고있는 객체입니까? 그것은 doesnt 루프 카운터/응답을 기반으로 숫자가 될 것 같습니다 – Kevin
'i'는 현재 개체를 말합니다. ctrl.contact "문법에서'ng-repeat ="(i, contact)를 사용할 수 있습니다.이 경우'i'는 현재 색인을 참조 할 것입니다. 또한 특별한 키워드'$ index'를 ['ngRepeat'] (https://docs.angularjs.org/api/ng/directive/ngRepeat) 지시어와 함께 사용할 수 있습니다. –
올바른 경우,이 경우'i'는 배열'ctrl.contact'의 각 요소를 나타냅니다. –