Angular 4 + Material Design chips in input

我怕爱的太早我们不能终老 提交于 2019-12-23 05:04:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!