AngularMaterialAutoComplete中的异步验证器无法注册错误。
创始人
2024-10-28 08:01:20
0

要在Angular Material AutoComplete中注册异步验证器的错误,可以在异步验证器中使用相应的FormControl。这可以通过在asyncValidator中手动添加错误来完成。以下是示例代码:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'autocomplete-example',
  templateUrl: 'autocomplete-example.html',
})
export class AutocompleteExample {
  form: FormGroup;
  myControl = new FormControl('', {
    asyncValidators: [this.exampleValidator],
  });

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.form = this.fb.group({
      myControl: this.myControl,
    });
  }

  exampleValidator(control: FormControl): Observable<{ [key: string]: any }> {
    return Observable.of([true]).delay(1000).map(() => {
      if (control.value === 'example') {
        control.setErrors({ 'exampleError': true });
      } else {
        control.setErrors(null);
      }
      return null;
    });
  }
}

在上面的代码中,我们可以使用FormControl的setErrors方法手动添加错误。如果值等于“example”,则会添加自定义错误“exampleError”。如果没有错误,则将设置错误为null。

这样,我们就可以在Angular Material AutoComplete中注册异步验证器的错误了。

相关内容

热门资讯

近期!德普之星私人局辅助免费,... 近期!德普之星私人局辅助免费,德普之星app安卓版破解版,切实是真的有挂(真是有挂)1、进入到德普之...
教程辅助挂!约局吧德州有挂吗,... 教程辅助挂!约局吧德州有挂吗,德普之星透视辅助软件下载,一直真的有挂(有挂工具);1、上手简单,内置...
程序员教你!wepoker分析... 程序员教你!wepoker分析,wejoker手机版透视脚本,真是是真的有挂(有挂分析)1、任何透视...
2026版方法!wepoker... 2026版方法!wepoker插件辅助,德扑之星的辅助工具介绍,都是真的是有挂(有挂教程)2026版...
科普常识!aapoker能控制... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
推出新举措!pokemmo脚本... 推出新举措!pokemmo脚本辅助器下载,德普之星透视辅助软件下载,一贯真的是有挂(确实有挂);1)...
今日焦点!wepoker安装教... 今日焦点!wepoker安装教程,拱趴大菠萝万能挂图解,一贯存在有挂(有挂秘籍)1、脚本辅助下载、免...
此事引发广泛关注!约局吧开挂,... 此事引发广泛关注!约局吧开挂,德普之星私人局辅助免费,果然真的是有挂(确实有挂)德普之星私人局辅助免...
现有关情况通报如下!有哪些免费... 现有关情况通报如下!有哪些免费的wpk作弊码,德普之星辅助器app,果然存在有挂(果真有挂)小薇(辅...
记者发布!hhpoker作弊实... 记者发布!hhpoker作弊实战视频,wpk德州局怎么透视,一直存在有挂(了解有挂)一、游戏安装教程...