Как мне выполнить автозавершение нескольких чипов на одной странице с использованием материала Angular 7.1.1?
Я использую Angular 6 с материалом Angular 7.1.1 И я пытаюсь использовать чип с автоматическим завершением. Но проблема в том, что когда я выбираю один из вариантов, он применяется ко всем чипам с автозаполнением.
`<mat-form-field class="example-chip-list">
<mat-chip-list #chipList>
<mat-chip
*ngFor="let fruit of fruits"
[selectable]="selectable"
[removable]="removable"
(removed)="remove(fruit)">
{{fruit}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input
placeholder="New fruit..."
#fruitInput
[formControl]="fruitCtrl"
[matAutocomplete]="auto1"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)">
</mat-chip-list>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
<mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
{{fruit}}
</mat-option>
</mat-autocomplete>
</mat-form-field>`
Как заставить его применять только к конкретному полю ввода?
1 ответ
Я думаю, вам просто нужно изменить id списка мат-чипов.
Итак, сначала вы устанавливаете идентификатор
<mat-chip-list #chipList>
и в вашу секунду вы устанавливаете идентификатор
<mat-chip-list #chipList2>
Затем вы устанавливаете
[matChipInputFor]="chipList"
а также [matChipInputFor]="chipList2"
Теперь все должно работать нормально.
Вы должны использовать разные списки для каждого списка чипов, а также два разных атрибута для [matAutocomplete]
HTML-код:
<mat-form-field class="example-chip-list">
<mat-chip-list #chipList>
<mat-chip *ngFor="let fruit of fruits" [selectable]="selectable" [removable]="removable" (removed)="remove(fruit)">
{{fruit}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input
placeholder="New fruit..."
#fruitInput
[formControl]="fruitCtrl"
[matAutocomplete]="auto"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)">
</mat-chip-list>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
<mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
{{fruit}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
<h2>Second Chips List</h2>
<mat-form-field class="example-chip-list">
<mat-chip-list #chipList>
<mat-chip
*ngFor="let fruit of fruits1"
[selectable]="selectable"
[removable]="removable"
(removed)="remove1(fruit)">
{{fruit}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input
placeholder="New fruit..."
#fruitInput1
[formControl]="fruitCtrl1"
[matAutocomplete]="auto1"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add1($event)">
</mat-chip-list>
<mat-autocomplete #auto1="matAutocomplete" (optionSelected)="selected1($event)">
<mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
{{fruit}}
</mat-option>
</mat-autocomplete>
</mat-form-field>