【问题标题】:Angular 4 + Material Design chips in inputAngular 4 + Material Design 芯片输入
【发布时间】:2017-07-19 21:02:19
【问题描述】:

我有可移动芯片的列表:

<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>

但我需要在输入或文本区域中创建它们,如本例所示:

https://material.angularjs.org/latest/demo/chips

我该怎么做?

【问题讨论】:

    标签: angular angular-material2


    【解决方案1】:

    Material2 的md-chip 没有Material1 成熟。 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

    【讨论】:

    猜你喜欢
    • 2017-12-23
    • 1970-01-01
    • 2018-01-11
    • 2023-03-19
    • 2017-11-17
    • 2016-09-20
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多