对于每对键值,显示键

时间:2018-05-04 14:56:54

标签: javascript angularjs angularjs-ng-repeat

此表单中有一个对象:

anObject = {name_1 : [4], 
            name_2 : [1], 
            name_3 : [5, 1, 2], 
            name_4 : [3, 4],  
};

在左侧,它是属性的名称,在右侧,它是一个数值值数组(一个或多个值)。

我想以某种方式向他们展示:

name_1 
  - 4
name_2
  - 1
name_3 
  - 5
  - 1
name_4
  - 3 
  - 4

我可以使用ng-repeat name_这样打印:

<div ng-repeat="(key, value) in $ctrl.anObject ">{{key}}</div>  

我也可以显示这样的数值:

<div ng-repeat="(key, value) in $ctrl.editAlertsByType">{{key}} - {{value}}</div>

但我想把它们放在钥匙下面。有可能吗?

2 个答案:

答案 0 :(得分:3)

你拉出的

value是一个数组,所以用第二个ng-repeat显示它。这是一个例子:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.anObject = {
    name_1: [4],
    name_2: [1],
    name_3: [5, 1, 2],
    name_4: [3, 4],
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <div ng-repeat="(key, value) in anObject">
    {{key}}
    <div ng-repeat="x in value">
      - {{x}}
    </div>
  </div>
</div>

答案 1 :(得分:0)

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">

<div ng-repeat="(key, value) in records">{{key}}
<div >{{value.toString()}}</div>
</div>


<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.records = {
    "key":[1],
    "key2":[1,2],
    "key3":[1,2,3]
  }
});
</script>
&#13;
&#13;
&#13;