2017-06-10 72 views
2

我有一個數組稱爲$scope.ticket_arrayNG-重複量內陣列

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

如何使用NG-重複相應地重複3次的數量值,仍然能夠表現出類型以及?

我嘗試了這些:

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

有什麼辦法可以訪問陣列內,重複的數量呢?

回答

2

您可以將數量傳遞給一個函數並生成一個數值數組,並對其進行ng-repeat。

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

DEMO

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)); 
 
}   
 
});
<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>

+0

是的,這我想要什麼謝謝! –

+0

對不起,我還在這裏再次感謝你 –