ng-重复数组内的数量

时间:2017-06-10 12:35:55

标签: javascript angularjs arrays

我有一个名为$scope.ticket_array的数组:

$scope.ticket_array = [{"type":"men junior", "quantity":"3"}];

如何使用ng-repeat相应的数量值重复3次,并且仍能显示类型?

我试过这些:

<div ng-repeat = "i in ticket_array">
          <p>{{i.type}}</p>
          <p>{{i.quantity}}</p>
</div>

有没有办法在数组内部访问并重复数量?

1 个答案:

答案 0 :(得分:2)

您可以将数量传递给函数并生成数值数组并对其进行重复操作。

$scope.getNumber = function(num) {
    return new Array(parseInt(num.quantity));   
}  

<强>样本

&#13;
&#13;
angular.module('myApp',[]).controller('studentController', function($scope){
 $scope.ticket_array = [{"type":"men junior", "quantity":"3"}];
 $scope.getNumber = function(num) {
    return new Array(parseInt(num.quantity));   
}        
 });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<body>
    <div ng-app="myApp" ng-controller="studentController">
     <div ng-repeat = "i in ticket_array">
      <div ng-repeat="j in getNumber(i) track by $index">
          <p>{{i.type}}</p>
          <p>{{j}}</p>
       </div>
    </div>
    </div>
</body>
&#13;
&#13;
&#13;