使用Angular ng-repeat迭代多层数组

时间:2017-06-09 05:54:13

标签: javascript angularjs arrays

我正在努力让阵列显示出我想要的样子。

我有:

$scope.array = [["one", "two", "three", "four"], "five"],["six", "seven", "three", "four"], "two"]];

我想迭代并显示第一个数组中的字符串。我想展示:

一二三四 六七三四。

目前我有以下内容:

<p ng-repeat="a in array">{{a[0]}}</p>

但它出现了:

["one", "two", "three", "four"]

["six", "seven", "three", "four"]

我已经尝试了一个过滤器来删除引号等,但它不断拉起另一个数组。

1 个答案:

答案 0 :(得分:6)

试试这个

<div ng-repeat="item in array">
  <div ng-repeat="a in item[0]">
    {{a}}
    </div>
</div>

,数组应该是这样的

 $scope.array =
 [
   [["one", "two", "three", "four"], "five"],
   [["six", "seven", "three", "four"], "two"]
 ];

var app = angular.module('app', []);
app.controller('main', ['$scope', function($scope) {
  $scope.array = [
    [
      ["one", "two", "three", "four"], "five"
    ],
    [
      ["six", "seven", "three", "four"], "two"
    ]
  ];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller=main>
  <div ng-repeat="item in array">
    <div ng-repeat="a in item[0]">
      {{a}}
    </div>
  </div>
</div>