我在 mat-dialog 中有一个显示多个字段的表单。它们中的大多数都是输入并完美地工作,它们与 [(ngModel)] 绑定,如下所示,但我想要一个带有 mat-chips 和自动完成的字段(如这里)。我的问题是我不知道如何将所选芯片与 [(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);
}
当我单击“添加”按钮时,我的项目与所有其他字段一起添加,但“movementsIds”字段为空,正如我们在控制台中看到的那样。
提前感谢您为我提供的任何帮助。