开发者问题收集

是否可以从 ValueChanges 切换到 (模糊) 以发出请求?

2019-04-04
2028

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

这是用于搜索的输入:

<input
  type="search"
  placeholder="Teclee un proveedor..."
  [formControl]="campoBusqueda"
  />

这是我在 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;
  }
});

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

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

在用户将焦点从输入中移开后发出请求,而不是每次用户在输入中输入内容时都发出请求。

我现在正在使用的 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;
      };
  }

现在的问题是我正在使用(模糊),输入的值在制表或单击输入后消失,当它到达第一个 if if (this.busqueda.length !== 0) { 时给出一个空值,在控制台中给出下一个堆栈跟踪:

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)
2个回答

使用 updateOn FormHooks

Reports the update strategy of the AbstractControl (meaning the event on which the control updates itself). Possible values: 'change' | 'blur' | 'submit' Default value: 'change

component.ts

 campoBusqueda = new FormControl('',{
    updateOn:'blur'
  });

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

Chellappan வ
2019-04-04

是的,它绝对适合您。您只需从 .ts 中删除 vlaueChanges 订阅,然后像这样调用您的搜索函数

在您的 .ts 文件中,只需保留此代码即可

searchForm : FormGroup;
this.searchForm = new FormGroup({
  searchField : new FormControl('')
})

myNewSearchMethod(){
  // This condition will check if there is some value in `searchField` or not
 if(this.searchForm.get('searchField ').value){
  // call your search function here
  }
}

<input
type="search"
placeholder="Teclee un proveedor..."
[formControl]="campoBusqueda"
(blur)="myNewSearchMethod()"
/>
Fahad Subzwari
2019-04-04