使用 http 服务的角度自定义验证 - 无法读取未定义的属性

Ryn9011

运行代码时出现此错误:无法读取未定义的属性“getClientEmails”。如果我从 ngOnInit 调用“clientService”,而不是从“shouldBeUnique”方法内部调用它,它会起作用

export class ClientFormComponent {

  emails: any = {};

  form = new FormGroup({
    email: new FormControl('',
      null,
      this.shouldBeUnique),

    password: new FormControl('', Validators.required),
    confirm: new FormControl('', Validators.required),
    firstname: new FormControl('', Validators.required),
    lastname: new FormControl('', Validators.required),
    address: new FormControl('', Validators.required),
    phone: new FormControl('', Validators.required),
    medical: new FormControl('', Validators.required)
  });

  constructor(public clientService: ClientService) { }


  shouldBeUnique(control: AbstractControl): Promise<ValidationErrors | null> {

    this.clientService.getClientEmails().subscribe(email => {
      this.emails = email;
      console.log(this.emails);
    });

    return new Promise((resolve, reject) => {

      if (control.value === '')
        resolve({ shouldBeUnique: true });
      else
        resolve(null);
    });
  }
}
JB尼泽

代替

this.shouldBeUnique

经过

(control: AbstractControl) => this.shouldBeUnique(control)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用$ http时无法读取未定义的属性'get'

无法使用$ http读取未定义的属性“成功”

使用Angular 7获取“无法读取未定义的属性'http'”

TypeError:无法读取未定义角度2的属性“ http”

HTTP 获取服务 - 无法读取未定义的属性

AngularJS Factory $ http无法读取未定义的属性“ length”

角4:无法读取未定义的属性“ http”

(Angular http $):无法读取未定义的属性“ length”

使用异步服务进行角度自定义验证

Angular 2-无法读取未定义的属性“ get”(http未定义)

无法使用 $mdToast 读取未定义的属性“显示”

使用SectionList时无法读取未定义的属性“ length”

使用SignalR“无法读取未定义的属性'client'” angularJS

TypeError:无法使用玩笑读取未定义的属性“原型”

无法使用 Angular 6 读取未定义的属性“冠军”

无法使用Angular读取未定义的属性“ get”

无法读取 JavaScript 类中未定义使用“This”的属性

使用 function() 时无法读取未定义的属性“控件”

TypeError:无法使用Angular读取未定义的属性“ comments”

使用 fetch 的 API - 无法读取未定义的属性“forEach”

无法使用JavaScript Promise读取未定义的属性'then'

无法使用ViewContainerRef读取未定义的属性'createComponent'

无法使用REACTJS读取未定义的属性“地图”

无法使用 Reduce 读取未定义的属性

TypeError:无法使用ReactJs读取未定义的属性“ map”

无法使用Reactjs Toastr读取未定义的属性成功

无法使用Promise读取未定义的属性'then'

无法使用React Hook读取未定义的属性“ map”

TypeError:无法读取未定义Express的属性“使用”