带有* ngFor的Angular2奇怪的表单行为

亚历克斯·贝格尼特

再次为那些看过我关于这种瓶装形式的帖子的人再次致敬……又回来了。

在我开始解释任何内容之前,这是一个工作的plunkr,显示了该问题:

工作朋克

注意:我typeId在第二个数组中显示Bottle ,它清楚地显示了问题。


问题 :

当我遇到以下情况的问题时,我正在检查绑定以确保正确选择了我选择的值:

  • 1:添加一些OrderReturn order以便您有多个输入。
  • 2:在这些输入中选择一些类型并设置一些值。
  • 3:如果删除了不是数组最后一个的任何输入,然后重新添加一个Input,则即使ngModel的绑定仍然正确,这些值也会被弄乱。

有没有办法避免使用标准这种奇怪的行为Template Driven Forms还是我必须经历ReactiveForms


我将在这里解释我的大部分代码:

我正在向寄送一排bottleArray装有气瓶name气瓶的数组,typeId格式为@Input()

我将这个数组及其对象深深克隆到两个单独的数组中:orderedClonedArrayBottlesreturnedClonedArrayBottles

然后,我将值添加到相应的显示数组中:orderedBottles并且returnedBottles现在具有一个count值。

...
@Input() private bottleArray: Bottle[];

private orderedClonedArrayBottles: Bottle[] = [];
private returnedClonedArrayBottles: Bottle[] = [];
private orderedBottles: BottleCommand[] = [];
private returnedBottles: BottleCommand[] = [];

ngOnChanges(changes) {
  // Get @Input data when it's ready
  if (changes.bottleArray) {
    // Cloning the Array AND the Bottles
    this.orderedClonedArrayBottles = this.deepClone(changes.bottleArray.currentValue);
    this.returnedClonedArrayBottles = this.deepClone(changes.bottleArray.currentValue);

    // Display first rows
    if (this.orderedClonedArrayBottles.length > 0) {
      this.orderedBottles.push(this.orderedClonedArrayBottles[0]);
    }
    if (this.returnedClonedArrayBottles.length > 0) {
      this.returnedBottles.push(this.returnedClonedArrayBottles[0]);
    }
  }
}

我可以使用以下方法删除任何数组的任何索引:

removeRow(index: number, type: string, event: Event): void {
  event.stopPropagation();
  if (type == 'order') {
    // Cleans the reference 'count' value.
    this.orderedBottles[index].count = null;
    this.orderedBottles.splice(index, 1);
  } else {
    this.returnedBottles[index].count = null;
    this.returnedBottles.splice(index, 1);
  }
}

我可以使用2个单独的按钮将这些项目推入这2个数组的任何一个中:AddOrder()AddReturnOrder()(相同代码):

addOrder(event: Event): void {
  event.stopPropagation();
  // Limits to the number of types
  if (this.orderedBottles.length < this.orderedClonedArrayBottles.length) {
    let index = this.getAvailableIndex(this.orderedBottles, this.orderedClonedArrayBottles);
    this.orderedBottles.push(this.orderedClonedArrayBottles[index]);
  }
}

为了避免在数组中推送现有的引用,我使用以下方法,该方法将为我提供第一个尚未显示的可用索引:

/**
 * Gets the first available index from 2 arrays containing the same references.
 *
 * @param {Object[]} displayedArray Array containing the occupied indexes
 * @param {Object[]} referenceArray Array containing all the indexes
 * @return {number} Index of the first available position
 */
getAvailableIndex(displayedArray: Object[], referenceArray: Object[]): number {
  let index: number = null;
  // Gets the available indexes of Bottles by filtering the referenceArray with the displayedArray
  let availablePositions: Object[] = referenceArray.filter(element => displayedArray.indexOf(element) < 0);
  // Return the first position available
  return index = referenceArray.indexOf(availablePositions[0]);
}

这是相应的HTML :(在Plunkr中更加清晰)

  <div fxLayout="row" style="max-width: 80%">
    <div fxLayout="column" style="min-width: 50%">

      <div fxLayout="row" style="max-width: 100%" *ngFor="let bottle of orderedBottles; let i = index">
        <md-select class="select" placeholder="Select bottle type" name="orderedTypeSelect_{{i}}" [(ngModel)]="orderedBottles[i].typeId">
          <md-option class="options" *ngFor="let type of bottleArray" [value]="type.typeId">
            {{ type.name }}
          </md-option>
        </md-select>

        <md-input-container class="container">
          <input md-input type="number" name="orderedBottleInput_{{i}}" autocomplete="off" [(ngModel)]="orderedBottles[i].count"
          step="1" min="0" max="99">
        </md-input-container>

        <button class="button-row" type="button" (click)="removeRow(i, 'order', $event)">-</button>

        {{orderedBottles[i].typeId}} -
        {{orderedBottles[i].count}}
      </div>

    </div>


    <div fxLayout="column" style="min-width: 50%">

      <div fxLayout="row" style="max-width: 100%" *ngFor="let bottle of returnedBottles; let j = index">
        <md-select class="select" placeholder="Select bottle type" name="returnedTypeSelect_{{j}}" [(ngModel)]="returnedBottles[j].typeId">
          <md-option class="options" *ngFor="let type of bottleArray; let z = index" [value]="bottleArray[z].typeId">
            {{ bottleArray[z].typeId }}
          </md-option>
        </md-select>

        <md-input-container class="container">
          <input md-input type="number" name="returnedBottleInput_{{j}}" autocomplete="off" [(ngModel)]="returnedBottles[j].count"
          step="1" min="0" max="99">
        </md-input-container>

        <button style="margin-top: -20px;" class="button-row" type="button" (click)="removeRow(j, 'return', $event)">-</button>

        {{returnedBottles[j].typeId}} -
        {{returnedBottles[j].count}}
      </div>

    </div>
  </div>

  <div class="margin">
    <button md-raised-button type="button" class="submit-button" (click)="addOrder($event)">Add Order</button>
    <button md-raised-button type="button" class="submit-button" (click)="addReturnOrder($event)">Add Return Order</button>
  </div>
yurzui

使用trackBy以避免混乱:

*ngFor="let bottle of orderedBottles; let i = index; trackBy: trackByFn"

trackByFn(index) {
    return index;
}

它还可以提高您的表现

改装柱塞

也可以看看

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章