使用angular来提取特定的Json数据

时间:2017-09-11 15:46:38

标签: javascript angularjs json angularjs-ng-repeat

有人可以指出我犯错的地方。

这是一个非常简单的应用程序,旨在打印出" name"字段输入和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>

我花了几个小时调整和编辑我的角度设置(应用程序,控制器等)很好。我很自信。

下面的代码段:

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

尝试以下方法:

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(0, 0),
    icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
    label: { color: '#00aaff', fontWeight: 'bold', fontSize: '14px', text: 'Your text here' }
});

答案 1 :(得分:0)

我会以不同的方式设置你的阵列。尝试这样的事情:

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。