【问题标题】:How to assign array of a particular index to another nested array?如何将特定索引的数组分配给另一个嵌套数组?
【发布时间】:2019-11-28 19:55:05
【问题描述】:

我在单击按钮时添加了一个材质扩展面板,并尝试将内容的值绑定到模型数组的每个索引中。我的数组中定义了另一个数组,这让我在分配嵌套数组的值时很头疼。第一个索引正确绑定,但数组的第二个和第三个索引重复。我也在做 2 路绑定,我会将其移除并将其绑定到服务。

第一个索引正确绑定,但数组的第二个和第三个索引重复。我也在做 2 路绑定,我会将其移除并将其绑定到服务。

<mat-card>
<mat-card-content>
  <div class="pesticide-info">
    <h2 class="h3 text-center">Pesticide Information</h2>
    <mat-accordion>
      <mat-expansion-panel *ngFor="let item of recordPesticideInfos; let i = index" [attr.data-index]="i">
        <mat-expansion-panel-header>
          <mat-panel-title>
            Pesticide Product
          </mat-panel-title>
          <mat-panel-description>
            Enter the Pesticide Information
          </mat-panel-description>
        </mat-expansion-panel-header>
        <div *ngFor="let pest of item.pesticideInfo; let j = index" [attr.data-index]="j">
          <mat-form-field [floatLabel]="auto">
            <mat-label>Product</mat-label>
            <!-- <input name=Product{{index}} [(ngModel)]="recordPesticideInfos[index].pesticideInfo[index].Product" placeholder="item">-->
            <input matInput placeholder="Product Name" [(ngModel)]="pest.Product" name=Product{{countOfProduct}}
              #Product="ngModel">                
          </mat-form-field>
        </div>
      </mat-expansion-panel>
      <div class="add-product text-center">
        <a class="btn btn-success text-white" (click)="addItem()" style="text-align: center"><i
            class="fa fa-plus"></i> Add Product</a>
      </div>
    </mat-accordion>
  </div>
</mat-card-content>

export class RecordPesticideInfo {
RecordPesticideInfosId: number;
    RecordId: number;
    PesticideInfoId: number;
    CountOfPestcideProduct: number;
    title: string;
    description: string;
    pesticideInfo: PesticideInfo[];

}

addItem() {   
// push object that you need to be added into array
this.recordPesticideInfos.push({
  RecordPesticideInfosId: null, RecordId: null,
  PesticideInfoId: null, CountOfPestcideProduct: this.countOfProduct++,
  title: "Pesticide Product", description: "Enter the Pesticide Information",
  pesticideInfo: this.pesticides as PesticideInfo[]
  // pesticideInfo: Object.keys(this.pesticides).map(i => this.pesticides[this.countOfProduct++])
});  

}

我想在每个按钮单击时绑定一个新的数组值 这是 stackblitz 网址:https://stackblitz.com/edit/angular-ylqkum

【问题讨论】:

  • 你能从中创建一个堆栈闪电战吗?
  • 我认为您可以使用扩展运算符获取副本,例如 pesticideInfo: [{...this.pesticides[0]}] 但我不确定您的代码

标签: javascript html arrays angular angular8


【解决方案1】:

问题出在这一行:

pesticideInfo: this.pesticides as PesticideInfo[]

每次添加新产品时,它都会从 this.pesticide 成员变量中获得完全相同的对象引用。

它适用于第一项的原因是您在那里创建了新对象内联:

pesticideInfo: [{
    PesticideInfoId: null,
    Product: null      
}]

我为新的默认产品创建添加了这个新功能:

  private createNewPesticide() {
    return {
      PesticideInfoId: null, 
      Product: null
    }
  }

检查工作的stackblitz

【讨论】:

  • 谢谢罗伯特!这现在有效。我太忙于处理索引而忽略了这一点。泰!
猜你喜欢
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
相关资源
最近更新 更多