以角度循环遍历JSONObject并仅打印某些值

时间:2017-09-13 12:00:54

标签: angularjs arrays json

嗨我有JSONObject,如下所示:

$scope.controls = {"fields":[{"id":1,"name":"Sales Figures 2017","area":"sales","value":"1800"},{"id":2,"name":"Are you on commission","area":"support","value":"Yes"}]}

我正在尝试循环并显示如下所示的结果,但只需要" name"和"价值"值。

输出就像

  1. 销售数字2017:1800
  2. 你在委托:是的
  3. 以下是我所做的只获取键和值,但它没有显示任何内容。

    <div ng-repeat="key in controls"> 
       {{key.name}} : {{key.value}}
    </div>
    

2 个答案:

答案 0 :(得分:2)

您必须访问控件对象中的fields,因为那是阵列所在的位置。

<div ng-repeat="key in controls.fields"> 
   {{key.name}} : {{key.value}}
</div>

答案 1 :(得分:1)

fields

访问ng-repeat媒体资源

angular.module("app",[])
.controller("ctrl",function($scope){
$scope.controls = {"fields":[{"id":1,"name":"Sales Figures 2017","area":"sales","value":"1800"},{"id":2,"name":"Are you on commission","area":"support","value":"Yes"}]}


})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <div ng-repeat="key in controls.fields"> 
   {{key.name}} : {{key.value}}
</div>
</div>