Angular Material Autocomplete force selection

后端 未结 6 1460
情书的邮戳
情书的邮戳 2021-02-07 09:18

In my angular 5 application I have some matAutocomplete, but I want to force the selection of oone of suggestions, so I am following this approach: stackblitz but for some reas

6条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 10:18

    Using blur event and matAutocomplete output event (optionSelected) we can force user to select option.

    
      
      
        {{item.Name}}
      
    
    

    ts file functions

    countryClick(event: any) {
      this.selectedCountry = event.option.value;
    }
    
    checkCountry() {
     setTimeout(()=> {
      if (!this.selectedCountry || this.selectedCountry !== this.signatureFormGroup.controls['country'].value) {
        this.signatureFormGroup.controls['country'].setValue(null);
        this.selectedCountry = '';
      }
     }, 1000);
    }
    

    depend on your requirement you can always delay the function which you call in blur or optionSelect event using setTimeout window function.

    setTimeout(()=> { 
    // function contents
    }, 1000);
    

提交回复
热议问题