动态生成的行中的数据绑定与下拉列表

问题描述 投票:0回答:1

我有一个表,它动态生成包含上传文件详细信息的行。行有一个下拉列表以选择文件类型。

我目前面临着动态生成的下拉列表的问题。如果我在任何一个下拉列表中选择一个值,它会为所有行中的所有下拉列表设置相同的值。

HTML:

<table>
 <thead>
  <tr>
   <th>File Name</th>
   <th>File Type</th>
  </tr>
 </thead>

 <tbody>
  <tr *ngFor="let item of uploader.queue">
   <td>
    {{ item?.file?.name }}
   </td>

   <td>
    <mat-form-field>
      <mat-select  placeholder="Select File Type" [(ngModel)]="selectedType">
        <mat-option *ngFor="let type of Types" [value]="type.type">
          {{type.type}}
        </mat-option>
       </mat-select>
     </mat-form-field>
   </td>
   <td>
    <button (click)="AddPdf(item)">Upload</button>
   </td>
  </tr>
 </tbody>
<table>

TS:

public AddPdf(Filenames: FileItem) {
  this.data = { filename: FileNames.file.name.toString() , LetterName:this.selectedLetterName, LetterType: this.selectedType };
  console.log(this.data.filename, 'File Name');
  console.log(this.data.LetterName, 'Letter Name');
  console.log(this.data.LetterType, 'Letter Type');
}

现在,如果我添加三个文件,则会生成三行。如果我选择一行的文件类型,则会反映所有行的文件类型。

请帮我理解这里的缺陷!

任何帮助表示赞赏。

angular typescript angular-material angular-ngmodel
1个回答
0
投票

我想出了问题。而不是[(ngModel)],我把它分成两部分,如下所示:

        <mat-form-field>
          <mat-select
            placeholder="Select Letter Name"
            #lettername
            [ngModel]="selectedLetterName"
            (ngModelChange)="onLetterNameSelect($event)"
            [ngModelOptions]="{standalone: true}">
            <mat-option *ngFor="let LetterName of LetterNames" [value]="LetterName.ReportName">
              {{LetterName.ReportName}}
            </mat-option>
          </mat-select>
        </mat-form-field>

它在改变之后按照我的预期运作。

© www.soinside.com 2019 - 2024. All rights reserved.