在Angular Material中,芯片占位符应该浮动而不是附加到末尾的解决方法如下所示:
mat-chip-list元素包裹住芯片组件,并设置multiple属性以允许多个芯片。
{{chip}}
cancel
chips数组来存储芯片的值,并实现添加和删除芯片的方法。import { Component } from '@angular/core';
@Component({
selector: 'app-chip-example',
templateUrl: 'chip-example.html',
styleUrls: ['chip-example.css'],
})
export class ChipExample {
chips: string[] = ['Apple', 'Banana', 'Orange'];
newChip: string;
addChip() {
if (this.newChip && !this.chips.includes(this.newChip)) {
this.chips.push(this.newChip);
this.newChip = '';
}
}
removeChip(chip: string) {
const index = this.chips.indexOf(chip);
if (index >= 0) {
this.chips.splice(index, 1);
}
}
}
通过上述代码,你可以在Angular Material中实现浮动的芯片占位符,并且可以动态添加和删除芯片。