【问题标题】:Getting Angular Autocomplete Value获取 Angular 自动完成值
【发布时间】:2020-05-02 17:54:38
【问题描述】:

所以我按照它的 Angular Material Autocomplete 的 Angular 文档进行了操作,但已经与从自动完成中获取选择的值进行了两天的斗争。基本上我想要的是自动完成显示人类的姓名和姓氏,但选项的值必须是整个人类对象。然后,我只想在选择 Human 时 console.log 记录 SelectedHuman。对此的任何解决方案都可能做到。

这是一个演示项目:https://stackblitz.com/edit/angular-hnu6uj

这是html文件:

<input [(ngModel)]="SelectedHuman" (change)="OnHumanSelected()" matInput [formControl]="MyControl" [matAutocomplete]="auto" placeholder="Human">

<mat-autocomplete #auto="matAutocomplete" [displayWith]="AutoCompleteDisplay">
  <mat-option *ngFor="let human of arrFilteredHumans | async" [value]="human">
    {{human.Name}} - {{human.Surname}}
  </mat-option>
</mat-autocomplete>

这里是 ts 文件: 导出类 AutocompleteDisplayExample 实现 OnInit {

  SelectedHuman: Human;
  MyControl = new FormControl();
  arrFilteredHumans: Observable<Human[]>;
  arrHumans = [
    new Human('1K59DN3', 27, 'John', 'Smith'),
    new Human('9VH23JS', 67, 'William', 'Shakespeare'),
    new Human('0QNF1HJ', 44, 'Elon', 'Musk')
  ];

  ngOnInit() {
    this.arrFilteredHumans = this.MyControl.valueChanges.pipe(
      startWith(''),
      map((val) => this.filter(val))
    );
  }

  filter(val: any): Human[] {
    return this.arrHumans.filter((item: any) => {
      //If the user selects an option, the value becomes a Human object,
      //therefore we need to reset the val for the filter because an
      //object cannot be used in this toLowerCase filter
      if (typeof val === 'object') { val = "" };
      const TempString = item.Name + ' - ' + item.Surname;
      return TempString.toLowerCase().includes(val.toLowerCase());
    });
  }

  AutoCompleteDisplay(item: any): string {
    if (item == undefined) { return }
    return item.Name + ' - ' + item.Surname;
  }

  OnHumanSelected() {
    console.log(this.MyControl); //This has the correct data
    console.log(this.MyControl.value); //Why is this different than the above result?
    console.log(this.SelectedHuman); //I want this to log the Selected Human Object
  }
}

export class Human {
  constructor(
    public ID: string,
    public Age: number,
    public Name: string,
    public Surname: string
  ) { }
}

【问题讨论】:

    标签: angular autocomplete angular-material


    【解决方案1】:

    点击事件和选择事件是两个不同的事件。你想要选择的事件,而不是点击事件,所以使用optionSelected from MatAutocomplete:

    <mat-autocomplete 
        #auto="matAutocomplete" 
        [displayWith]="AutoCompleteDisplay"
        (optionSelected)="OnHumanSelected($event.option)">
    
        <!-- 
        ... 
        -->
    
    </mat-autocomplete>
    
    OnHumanSelected(option: MatOption) {
        console.log(option.value);
    }
    

    【讨论】:

      【解决方案2】:

      这是Stackblitz link

      这是代码

         OnHumanSelected(SelectedHuman) {
          console.log(SelectedHuman); // get from view
          console.log(this.SelectedHuman); // get from variable
        }
      

      HTML 文件

        <input [(ngModel)]="SelectedHuman"  matInput [formControl]="MyControl" [matAutocomplete]="auto" placeholder="Human">
      
      <mat-autocomplete #auto="matAutocomplete" [displayWith]="AutoCompleteDisplay">
          <mat-option (click)="OnHumanSelected(SelectedHuman)"  *ngFor="let human of arrFilteredHumans | async" [value]="human">
          {{human.Name}} - {{human.Surname}}
        </mat-option>
      </mat-autocomplete>
      

      【讨论】:

        【解决方案3】:

        所以这似乎解决了问题。将 OnHumanSelected() 函数放在 mat-option 的 (click) 事件上:

          <mat-option  (click)="OnHumanSelected()" *ngFor="let human of arrFilteredHumans | async" [value]="human">
            {{human.Name}} - {{human.Surname}}
          </mat-option>
        
          ####################
        
          OnHumanSelected() {
            console.log(this.SelectedHuman);
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-25
          • 1970-01-01
          • 2012-12-02
          • 2012-02-12
          • 1970-01-01
          相关资源
          最近更新 更多