ng重复物品和物业重复项目?

时间:2015-07-30 04:33:54

标签: javascript angularjs angularjs-ng-repeat ng-repeat

我需要重复我拥有的所有项目,但我需要通过"数量"重新重复项目。属性。

$scope.products = [{name: 'Mouse', price: 2, quantity: 3},
                   {name: 'item2', price: 5, quantity: 2}]

我需要获得鼠标项目的最终HTML 3(数量)div,item2的2个div:

<div>
 Mouse<br/>
 Price: $2 <br/>
</div>
<div>
Mouse<br/>
Price: $2 <br/>
</div>
<div>
Mouse<br/>
Price: $2 <br/>
</div> //need Mouse be repeated 3 times by they quantity property

2 个答案:

答案 0 :(得分:2)

以这种方式:

    <ul style="color: white">
        <li ng-repeat="product in products">
            <ul>
                <li ng-repeat="t in times(product.quantity) track by $index">{{product.name}}</li>
            </ul>
        </li>
    </ul>

在控制器中:

    $scope.products = [{name: 'Mouse', price: 2, quantity: 3},
               {name: 'item2', price: 5, quantity: 2}];
    $scope.times = function(x) {
        return new Array(x);
    };

fiddle

答案 1 :(得分:0)

@neomartin你可以根据数量显示价格

  

// html

<div ng-app>
 <div ng-controller="ClickToEditCtrl">
    <p>Your Data:</p>
    <ul ng-repeat="(key, value) in products">
        name: {{value.name}} :  Quantity {{value.quantity}} <br/>
              price: {{value.price * value.quantity | currency }}
    </ul>
</div>

  

// js

function ClickToEditCtrl($scope) {

    $scope.products = [{name: 'Mouse', price: 2, quantity: 3},
               {name: 'item2', price: 5, quantity: 2}]
 }

以下是:Jsfiddle我希望这会有所帮助