如何使用 Material 将 mat-chip 绑定到 Angular 6 中的 ngmodel

How to bind mat-chip to ngmodel in Angular 6 with Material

我在 mat-dialog 中有一个显示多个字段的表单。它们中的大多数都是输入并且工作完美,它们与 [(ngModel)] 绑定,如下所示,但我想要一个带有 mat-chips 和自动完成的字段(如 here)。我的问题是我不知道如何将所选芯片与 [(ngModel)] 绑定以获取数据。

我的HTML:

<form class="mat-dialog-content" (ngSubmit)="submit" #formControl="ngForm">
<div class="form">
  <mat-form-field color="accent">
    <input matInput #inputname class="form-control" placeholder="Nom du WOD" [(ngModel)]="data.name" name="name" maxlength="50" required >
    <mat-error *ngIf="formControl.invalid">{{getErrorMessage()}}</mat-error>
    <mat-hint align="end">{{inputname.value?.length || 0}}/50</mat-hint>
  </mat-form-field>
</div>

<div class="form">
  <mat-form-field color="accent">
    <input matInput placeholder="Notes du coach" [(ngModel)]="data.coachesNotes" name="coachesNotes">
  </mat-form-field>
</div>

<div class="form">
  <mat-form-field class="chip-list" aria-orientation="horizontal">
    <mat-chip-list #chipList [(ngModel)]="data.movementsIds" name="movementsIds">
      <mat-chip
        *ngFor="let mouv of mouvs"
        [selectable]="selectable"
        [removable]="removable"
        (removed)="remove(mouv)">
        {{mouv}}
        <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
      </mat-chip>
      <input
        placeholder="Ajouter un mouvement..."
        #mouvInput
        [formControl]="mouvCtrl"
        [matAutocomplete]="auto"
        [matChipInputFor]="chipList"
        [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
        [matChipInputAddOnBlur]="addOnBlur"
        (matChipInputTokenEnd)="add($event)"
      />
    </mat-chip-list>
    <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selectedChip($event)">
      <mat-option *ngFor="let mouv of filteredMouvs | async" [value]="mouv">
        {{ mouv }}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</div>

<div mat-dialog-actions>
  <button mat-button [type]="submit" [disabled]="!formControl.valid" [mat-dialog-close]="1" (click)="confirmAdd()">Ajouter</button>
  <button mat-button (click)="onNoClick()" tabindex="-1">Annuler</button>
</div></form>

我的组件

public confirmAdd(): void {
  this.dataService.addWod(this.data);
}

View of my filled form

当我单击 "add" 按钮时,我的项目会添加所有其他字段,但 "movementsIds" 字段为空,正如我们所见 in the console.

在此先感谢您能给我带来的任何帮助。

对于那些可能遇到同样问题的人,我找到了解决方案。

我没有使用 [(ngModel)] 将芯片列表绑定到我的 "data.movementsIds",而是将 "mouvs" 数组传递给我的 "confirmAdd" 函数,然后设置 data.movementsIds 这样的数组:

HTML :

  <mat-form-field class="chip-list" aria-orientation="horizontal">
    <mat-chip-list #chipList>
      <mat-chip
        *ngFor="let mouv of mouvs"
        [removable]="removable"
        (removed)="remove(mouv)">
        {{mouv}}
        <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
      </mat-chip>
      <input
        placeholder="Ajouter un mouvement..."
        #mouvInput
        [formControl]="movementsIds"
        [matAutocomplete]="auto"
        [matChipInputFor]="chipList"
        [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
        [matChipInputAddOnBlur]="addOnBlur"
        (matChipInputTokenEnd)="add($event)"/>
    </mat-chip-list>
    <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selectedChip($event)">
      <mat-option *ngFor="let mouv of filteredMouvs | async" [value]="mouv">
        {{ mouv }}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>

分量:

public confirmAdd(mouvs: Array<any>): void {
   this.data.movementsIds = JSON.stringify(mouvs);
   this.dataService.addWod(this.data);
}