【问题标题】:Angular4 Material chipset+autocompleteAngular4 Material 芯片组+自动完成
【发布时间】:2019-09-20 18:33:04
【问题描述】:

我正在使用 angular 4 的芯片组和自动完成功能在 stackoverflow 中开发诸如标记系统之类的功能。这是我写的一段代码。它不工作

<mat-form-field class="col-md-4 col-md-offset-2">
                  <mat-chip-list #chipList>
                    <mat-chip *ngFor="let item of displayItems" [selectable]="selectable"
                             [removable]="removable" (remove)="remove(item)">
                      {{item}}
                      <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
                    </mat-chip>
                    <input placeholder="Enter Items..."  
                           [matChipInputFor]="chipList"
                           [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                           [matChipInputAddOnBlur]="addOnBlur"
                           (matChipInputTokenEnd)="add($event)" matInput  [matAutocomplete]="auto"/>

                           <mat-autocomplete #auto="matAutocomplete">
                                <mat-option *ngFor="let option of options" [value]="option">
                                    {{ option }}
                                </mat-option>
                            </mat-autocomplete>

                  </mat-chip-list>
                  <mat-hint align="end">Press comma or enter after each selection</mat-hint>
                </mat-form-field>

以下是在 TS 文件中:选项基本上是自动完成将选择的位置。

snacksType: String[];

  visible = true;
  selectable = true;
  removable = true;
  addOnBlur = true;
  options=['banana','apple','jackfruit','mango', 'grapes', 'kiwi'];
  // Enter, comma
  separatorKeysCodes = [ENTER, COMMA];

  displayItems = [];

add(event: MatChipInputEvent): void {
      let input = event.input;
      let value = event.value;

      // Add our item
      if ((value || '').trim()) {
        this.displayItems.push(value.trim());
      }

      // Reset the input value
      if (input) {
        input.value = '';
      }
    }

    remove(item: any): void {
      let index = this.displayItems.indexOf(item);

      if (index >= 0) {
        this.displayItems.splice(index, 1);
      }
    }

【问题讨论】:

    标签: angular angular-material md-autocomplete md-chip mat-autocomplete


    【解决方案1】:

    您可以在“mat-autocomplete”中使用@Output(optionSelected)并推送新项目。

    snacksType: String[];
    
      visible = true;
      selectable = true;
      removable = true;
      addOnBlur = true;
      options=['banana','apple','jackfruit','mango', 'grapes', 'kiwi'];
      // Enter, comma
      separatorKeysCodes = [ENTER, COMMA];
    
      displayItems = [];
    
    add(event: MatChipInputEvent): void {
          let input = event.input;
          let value = event.value;
    
          // Add our item
          if ((value || '').trim()) {
            this.displayItems.push(value.trim());
          }
    
          // Reset the input value
          if (input) {
            input.value = '';
          }
        }
    
        remove(item: any): void {
          let index = this.displayItems.indexOf(item);
    
          if (index >= 0) {
            this.displayItems.splice(index, 1);
          }
        }
    
    public addSelect(event) {
        let option = event.option;
        let value = option.value;
        if ((value || '').trim()) {
          this.fruits.push({ name: value.trim() });
        }
    }
    

    在您的 ma​​t-autocomplete 中添加 (optionSelected)="addSelect($event)"

    <mat-form-field class="col-md-4 col-md-offset-2">
                      <mat-chip-list #chipList>
                        <mat-chip *ngFor="let item of displayItems" [selectable]="selectable"
                                 [removable]="removable" (remove)="remove(item)">
                          {{item}}
                          <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
                        </mat-chip>
                        <input placeholder="Enter Items..."  
                               [matChipInputFor]="chipList"
                               [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                               [matChipInputAddOnBlur]="addOnBlur"
                               (matChipInputTokenEnd)="add($event)" matInput  [matAutocomplete]="auto"/>
    
                               <mat-autocomplete #auto="matAutocomplete" (optionSelected)="addSelect($event)">
                                    <mat-option *ngFor="let option of options" [value]="option">
                                        {{ option }}
                                    </mat-option>
                                </mat-autocomplete>
    
                      </mat-chip-list>
                      <mat-hint align="end">Press comma or enter after each selection</mat-hint>
                    </mat-form-field>
    

    【讨论】:

      【解决方案2】:

      它不起作用,因为您没有对我们的输入添加任何控制以检测值变化

      <input placeholder="Enter Items..."  
                             [formControl]="displayCtrl"
                             [matChipInputFor]="chipList"
                             [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                             [matChipInputAddOnBlur]="addOnBlur"
                             (matChipInputTokenEnd)="add($event)" matInput  [matAutocomplete]="auto"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-06
        • 2017-11-09
        • 2020-09-07
        • 1970-01-01
        • 1970-01-01
        • 2017-12-28
        • 1970-01-01
        • 2017-08-29
        相关资源
        最近更新 更多