防止在自动完成 setValue 中触发值更改

Sub*_*jee 2 angular

我创建了一个自动完成和一个选项组件,当我们在输入字段中输入搜索短语时,我调用 Web 服务并获取数据,然后在选项组件中显示列表,但是当我从该列表中选择一个选项并设置值时到输入字段,然后 Web 服务全部再次触发。我怎样才能防止这种情况发生?示例是材料设计自动完成的工作原理,我在从选项列表中选择某些内容时想要类似的功能。谢谢。

<app-rx-search-field>
          <input id="rx-search-field" type="search" class="form-control __search_field" name="search" placeholder="Search" autocomplete="off" [formControl]="providerSchoolSearchCtrl" formControlName="school" />
          <app-rx-options [optionsList]="suggestedSchoolsList$ | async" [key]="'school.name'" (optionSelected)="onOptionSelected($event)"></app-rx-options>
        </app-rx-search-field>
Run Code Online (Sandbox Code Playgroud)
this.providerSchoolSearchCtrl.valueChanges
      .pipe(
        debounceTime(500),
        distinctUntilChanged(),
        mergeMap(search => of(search).pipe(
          delay(500)
        ))
      ).subscribe({
      next: value => {
        this.store.dispatch(new GetSchoolRequest({isRequesting: true, searchPhrase: value}));
      }
    });
Run Code Online (Sandbox Code Playgroud)
public onOptionSelected = (event: any) => {
    console.log(event);
// This is to set the value for the input field
    this.providerSchoolSearchCtrl.setValue(event.option['school.name']);
    this.providerEducation.controls['school'].setValue(event.option['school.name'], {emitEvent: false});
  }
Run Code Online (Sandbox Code Playgroud)

Nit*_*jan 6

当将值设置为控件时,您可以尝试此选项。该setValue方法有第二个参数,我们可以通过它控制是否应该发出事件。

 this.providerSchoolSearchCtrl.setValue(event.option['school.name'], {emitEvent: false});
Run Code Online (Sandbox Code Playgroud)

查看角度文档: https: //angular.io/api/forms/FormControl#setValue