2017-07-19 45 views

回答

1

材質2的md-chip不如材質1成熟。 Material2團隊正在努力增加許多這些輸入字段功能,您可以檢查他們的latest example in github。可能他們會添加beta.9版本。

所以,現在md-chipmd-input需要手動構建。

下面是我可以最接近Material1示例的示例。

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

+0

謝謝你這麼多 – user

+0

prolly與β-9是=> https://github.com/angular/material2/問題/ 6328 – Pascal