问题
I have list of removable chips:
<md-chip-list>
<md-chip *ngFor="let chip of chips; let i = index"
color="accent">
{{chip}}
<i class="fa fa-close" (click)="remove(i)"></i>
</md-chip>
</md-chip-list>
But I need to create them in input or in text area like in this example:
https://material.angularjs.org/latest/demo/chips
How can I do that?
回答1:
Material2's md-chip
is not as mature as Material1. Material2 team is working on adding a lot of those input field features, you can check their latest example in github. Probably they will add them with beta.9 release.
So, for now md-chip
with md-input
needs to manually constructed.
Here's the example that I could get closest to the Material1's example.
html:
<md-input-container floatPlaceholder="never">
<md-chip-list mdPrefix>
<md-chip *ngFor="let chip of chips; let i = index"
color="accent">
{{chip}}
<i class="fa fa-close" (click)="remove(i)"></i>
</md-chip>
</md-chip-list>
<input mdInput [mdDatepicker]="picker"
placeholder="Enter fruit"
[(ngModel)]="chipValue"
#chip
(keydown.enter)="add(chip.value)"
(keydown.backspace)="removeByKey(chip.value)">
</md-input-container>
ts:
chipValue: string;
chips = [
'Apple',
'Orange',
'Banana',
'Mango',
'Cherry'
]
remove(item){
this.chips.splice(item, 1);
}
add(value){
this.chips.push(value);
this.chipValue = "";
}
removeByKey(value){
if(value.length < 1){
if(this.chips.length > 0){
this.chips.pop();
}
}
}
Plunker demo
来源:https://stackoverflow.com/questions/45200931/angular-4-material-design-chips-in-input