Angular Material Autocomplete force selection

后端 未结 6 1476
情书的邮戳
情书的邮戳 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条回答
  •  我寻月下人不归
    2021-02-07 10:21

    I found this helpful:

    private subscribeToClosingActions(): void {
    if (this.subscription && !this.subscription.closed) {
      this.subscription.unsubscribe();
    }
    
    this.subscription = this.autoCompleteTrigger.panelClosingActions
      .subscribe((e) => {
          if (!e || !e.source) {
            const selected = this.matAutocomplete.options
              .map(option => option.value)
              .find(option => option === this.formControl.value);
    
            if (selected == null) {
              this.formControl.setValue(null);
            }
          }
        },
        err => this.subscribeToClosingActions(),
        () => this.subscribeToClosingActions());
    }
    

提交回复
热议问题