[英]Angular Material mat-chips not setting to be removable
Problem Statement问题陈述
My problem is that when I am using Angular Material's mat-chip
, it seems it cannot be set as removable even though I set it to true.我的问题是,当我使用 Angular Material 的
mat-chip
,即使我将其设置为 true,它似乎也无法设置为可移动。
Code代码
<mat-form-field>
<input matInput [matChipInputFor]="chipList" (matChipInputTokenEnd)="addOption($event)"
type="number" maxlength="4" placeholder="Enter an amount here">
</mat-form-field>
<mat-chip-list>
<mat-chip #chipList *ngFor="let option of options" [removable]="true"
(removed)="removeOption(option)">{{option}}
</mat-chip>
</mat-chip-list>
RemoveOption Method RemoveOption 方法
removeOption(option: String) {
const index = this.options.indexOf(option);
if (index >= 0) {
this.options.splice(index, 1);
}
}
Explanation of Code代码说明
As you can see, I have set [removable]
to true and (removed)
with a removeOption
method.如您所见,我已使用
removeOption
方法将[removable]
设置为 true 并(removed)
。 These things do not work for some strange reason.由于某些奇怪的原因,这些东西不起作用。
I copied the example from here: https://material.angular.io/components/chips/examples , the section with the example is named: "Chips with Input"我从这里复制了示例: https://material.angular.io/components/chips/examples ,示例部分命名为:“带输入的芯片”
Actual Output实际产量
The chips show no little remove button on the right:芯片在右侧显示了很多删除按钮:
Expected Output预期产出
The chips with a little remove button on the right:右边有一个小的移除按钮的芯片:
You have to add the mat-icon
to trigger the removal.您必须添加
mat-icon
才能触发删除。 In the material example you have:在材料示例中,您有:
<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>
This contains the action to trigger matChipRemove
inside the mat-icon
.这包含在
mat-icon
触发matChipRemove
的操作。
Link for demo: https://stackblitz.com/angular/mjygopomxvq?file=src%2Fapp%2Fchips-autocomplete-example.html演示链接: https : //stackblitz.com/angular/mjygopomxvq?file= src%2Fapp%2Fchips-autocomplete- example.html
You're not able to see the icon because the section <mat-icon matChipRemove>cancel</mat-icon>
is missing from the code.您无法看到该图标,因为代码中缺少
<mat-icon matChipRemove>cancel</mat-icon>
部分。
Your code should look like this:您的代码应如下所示:
<mat-chip-list>
<mat-chip #chipList *ngFor="let option of options" [removable]="true"
(removed)="removeOption(option)">{{option}}
<mat-icon matChipRemove>cancel</mat-icon>
</mat-chip>
</mat-chip-list>
you are missing the icon tag.你缺少图标标签。
<mat-chip-list>
<mat-chip #chipList *ngFor="let option of options" [removable]="true"
(removed)="removeOption(option)">{{option}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
</mat-chip-list>
Noticed that if the mat-icon or any other element that does matChipRemove has style "pointer-events: none;"请注意,如果 mat-icon 或执行 matChipRemove 的任何其他元素具有样式“pointer-events: none;” then click on the mat-icon (X) doesn't remove the chip.
然后单击垫子图标 (X) 不会移除芯片。 The chip still can be removed by the keyboard backspace button.
芯片仍然可以通过键盘退格按钮移除。 Solution - set "pointer-events: auto;"
解决方案——设置“pointer-events: auto;”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.