首页 文章

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

提问于
浏览
0

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

My 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>

My component :

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

View of my filled form

当我单击"add"按钮时,我的项目将添加所有其他字段,但"movementsIds"字段为空,因为我们可以看到in the console .

提前感谢您提供的任何帮助 .

1 回答

  • 1

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

    而不是使用[(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>
    

    Component :

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

相关问题