使用 angular 提取特定的 Json 数据

Using angular to extract specific Json data

谁能指出我哪里出错了。

这是一个非常简单的应用程序,旨在打印出 Json 对象数组中的 "name" 字段,这是通过以下行完成的:

{{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>

经过几个小时的调整和编辑后,我确信我的 angular 设置(应用程序、控制器等)没问题。

下面的代码片段:

<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>

尝试以下操作:

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

我会以稍微不同的方式设置您的数组。尝试这样的事情:

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"
              }
         }
    ]
}

然后在你的 ng-repeat 中,尝试这样的事情:

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

如果您要跟踪数组中项目的索引,请使用 $index,而不是 i。