Angular JS Multiple ngRepeat

时间:2017-07-10 09:14:42

标签: javascript angularjs

我正在尝试执行以下操作:当您单击“添加包”时,它会将数据推送到数组,我可以在其中指定例如套餐一,但在其中,我想推送多个项目(产品)“添加项目”。问题是当我创建一个新的包它可以工作,但每个包中的项是相同的,所以当我向任一动态包添加更多时,其中的项是相同的。我假设这是因为每次都是相同的数组,我如何通过索引跟踪它?

HTML:

<div class="col-12 order-box" ng-repeat="orderpackage in orderproductspackages track by $index">
  <div class="col-12">
    <label for="name_{{$index}}">Package Name</label>
    <input type="text" ng-model="orderpackage.orderpackagename" name="name_{{$index}}" required>                                                 
  </div>
  <div class="col-12" ng-repeat="ordercontent in orderproductscontents track by $index">                       
    <div class="col-2">
      <label for="name_{{$index}}">Product</label>
      <select ng-model="ordercontent.ordercontentname" name="name_{{$index}}" ng-change="onProductChange(ordercontent.ordercontentname,$index)">
        <option ng-repeat="product in productResponse | orderBy:'productname'" value="{{product.productname}}">{{product.productname}}</option>
      </select>                                                                            
    </div>                                   
    <div class="col-2">
      <label for="name_{{$index}}">Quantity</label>
      <input type="text" ng-model="ordercontent.ordercontentquantity" name="name_{{$index}}" ng-keyup="onProductChange(ordercontent.ordercontentname,$index)" required>
    </div>                     
    <div class="col-2">
      <label for="name_{{$index}}">Price</label>
      <input type="text" ng-model="ordercontent.ordercontentprice" name="name_{{$index}}" ng-change="onPriceChange(ordercontent.ordercontentname,$index)" disabled required>                                                 
    </div>
    <div class="col-2">
      <label for="name_{{$index}}">Per {{ordercontent.ordercontentmeasurement}}</label>
      <input type="text" ng-model="ordercontent.ordercontentpriceper" name="name_{{$index}}" disabled required>                                                 
    </div>
    <div class="col-2">
      <label for="name_{{$index}}">Total</label>
      <input type="text" ng-model="ordercontent.ordercontenttotal" name="name_{{$index}}" required disabled>
    </div>
    <div class="col-2">
      <label for="name_{{$index}}">Options</label>
      <a class="button critical small" ng-click="removeOrderContent($index)"><span class="fa fa-minus"></span> Remove</a> 
    </div>                    
  </div>
  <div class="col-12 m20-top p15 text-center add-api" ng-click="addOrderContent()">
    <span class="fa fa-plus"></span> Add Item</a>
  </div>
</div>
<div class="col-12 m20-top p15 text-center add-api" ng-click="addOrderPackage()">
  <span class="fa fa-plus"></span> Add Package</a>
</div>

代码链接:

_this.addOrderPackage = function() {
  _this.orderproductspackages.push({ ordernumber:_this.order.ordernumber });
}

_this.addOrderContent = function() {
  _this.orderproductscontents.push({                    
    ordercontentname:'',                   
    ordercontentquantity:'1',                 
    ordercontentprice:'',
    ordercontentpriceper:'',
    ordercontenttotal:''
  });
}

1 个答案:

答案 0 :(得分:1)

当然,包中的所有项都是相同的,因为您在包中使用相同的内容数组:ng-repeat="ordercontent in orderproductscontents"。如果我理解你,你需要这个数组orderproductscontents在每个包中都是独一无二的。为此,将此数组存储在包对象中,如下所示:

_this.addOrderPackage = function() {
  _this.orderproductspackages.push({ 
    ordernumber: _this.order.ordernumber,
    contents: []
  });
}

然后只是ng-repeat="ordercontent in orderpackage. contents"