是否可以从 ValueChanges 交换到 (blur) 以提出请求?

art*_*uro 1 firebase angularfire2 angular

我问这个是因为我开始学习一些基本的角度课程,并且我正在使用一些过滤器,在我使用 ValueChanges 输入时将请求发送到 firebase:

这是用于搜索的输入:

<input
  type="search"
  placeholder="Teclee un proveedor..."
  [formControl]="campoBusqueda"
  />
Run Code Online (Sandbox Code Playgroud)

这是我必须使用的代码ngOnInit()

this.campoBusqueda = new FormControl();
this.campoBusqueda.valueChanges.subscribe(term => {
  this.busqueda = term;
  this.cargando = true;
  if (this.busqueda.length !== 0) {
    this.proveedoresService
      .getProveedoresSearch(this.busqueda)
      .subscribe(proveedores => {
        this.proveedores = [];
        // tslint:disable-next-line: forin
        for (const id$ in proveedores) {
          const p = proveedores[id$];
          p.id$ = id$;
          this.proveedores.push(proveedores[id$]);
        }
        if (this.proveedores.length < 1 && this.busqueda.length >= 1) {
          this.noresultados = true;
          this.proveedoresService
            .getProveedores()
            .subscribe( proveedores => {
              this.proveedores = [];
              // tslint:disable-next-line: forin
              for (const id$ in proveedores) {
                const p = proveedores[id$];
                p.id$ = id$;
                this.proveedores.push(proveedores[id$]);
              }
            });
        } else {
          this.noresultados = false;
        }
      });
    this.cargando = false;
    this.resultados = true;
  } else {
    this.proveedores = [];
    this.cargando = false;
    this.resultados = false;
  }
});
Run Code Online (Sandbox Code Playgroud)

现在我想知道的是是否可以使用(模糊):

    <input
    type="search"
    placeholder="Teclee un proveedor..."
    [formControl]="campoBusqueda"
    (blur)="myNewSearchMethod()"
    />
Run Code Online (Sandbox Code Playgroud)

在用户从输入中获取焦点后发出请求,而不是每次用户在输入中键入内容时发出请求。

myNewSearchMethod()正在使用的正确知识是:

    myNewSearchMethod() {
    this.campoBusqueda = new FormControl();
    this.busqueda = this.campoBusqueda.value;
    this.cargando = true;
    if (this.busqueda.length !== 0) {
        this.proveedoresService
          .getProveedoresSearch(this.busqueda)
          .subscribe(proveedores => {
            this.proveedores = [];
            // tslint:disable-next-line: forin
            for (const id$ in proveedores) {
              const p = proveedores[id$];
              p.id$ = id$;
              this.proveedores.push(proveedores[id$]);
            }
            if (this.proveedores.length < 1 && this.busqueda.length >= 1) {
              this.noresultados = true;
              this.proveedoresService
                .getProveedores()
                .subscribe( proveedores => {
                  this.proveedores = [];
                  // tslint:disable-next-line: forin
                  for (const id$ in proveedores) {
                    const p = proveedores[id$];
                    p.id$ = id$;
                    this.proveedores.push(proveedores[id$]);
                  }
                });
            } else {
              this.noresultados = false;
            }
          });
        this.cargando = false;
        this.resultados = true;
      } else {
        this.proveedores = [];
        this.cargando = false;
        this.resultados = false;
      };
  }
Run Code Online (Sandbox Code Playgroud)

现在的问题是,当我使用 (blur) 时,如果if (this.busqueda.length !== 0) {在控制台中提供下一个堆栈跟踪,则输入的值在 Tab 键或单击输入后在到达第一个时给出空值后消失:

ProveedoresComponent.html:5 ERROR TypeError: Cannot read property 'length' of null
at ProveedoresComponent.push../src/app/proveedores/proveedores/proveedores.component.ts.ProveedoresComponent.myNewSearchMethod (proveedores.component.ts:170)
at Object.eval [as handleEvent] (ProveedoresComponent.html:9)
at handleEvent (core.js:23097)
at callWithDebugContext (core.js:24167)
at Object.debugHandleEvent [as handleEvent] (core.js:23894)
at dispatchEvent (core.js:20546)
at core.js:20993
at HTMLInputElement.<anonymous> (platform-browser.js:993)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:17280)
Run Code Online (Sandbox Code Playgroud)

Che*_*pan 5

使用updateOn FormHooks

报告 AbstractControl 的更新策略(意味着控件更新自身的事件)。可能的值:'改变' | '模糊' | '提交' 默认值:'更改

组件.ts

 campoBusqueda = new FormControl('',{
    updateOn:'blur'
  });
Run Code Online (Sandbox Code Playgroud)

示例:: https://stackblitz.com/edit/angular-5e7qd2