简体   繁体   English

Angular 8 在另一个函数中调用一个函数

[英]Angular 8 call a function in another function

I have made a FormGroup in my Angular app like this:我在我的 Angular 应用程序中创建了一个 FormGroup,如下所示:

  registrationForm: FormGroup;
  this.registrationForm = this.fb.group({
  orga: ['', [Validators.required, this.organismeValidator.bind(this)]],
  });

But my problem is that, when I enter in the function "dateFin", I want to recall the organismeValidator of my form.但我的问题是,当我输入函数“dateFin”时,我想调用我的表单的有机验证器。 But it seems to never rechange the error of my"OrganismeValidator".但它似乎永远不会改变我的“OrganismeValidator”的错误。 For exemple, when "myDtFin" is not null, I would like to return a null error for this control, And in my case, it never changes"例如,当“myDtFin”不为空时,我想为此控件返回一个空错误,在我的情况下,它永远不会改变”

    organismeValidator(control: AbstractControl) {
    // this.serviceHttp.getAllPensions().subscribe(resp => {
    // this.pensionsInvalidite = resp;
    if (this.pensionsInvalidite) {
      for (const pension of this.pensionsInvalidite) {
        if (!pension.dtFin) {
          if (control.value === pension.organisme.code && !this.myDtFin) {
            console.log('yes');
            return { organismeValidator: true };
          }
        }
      }
    }
    return null;
  }

  datefin(myDtFin) {
    this.myDtFin = myDtFin;
    this.organismeValidator.call(this.myForm);

    }

please return null if it's valid other wise return validation error as an object.如果有效,请返回 null 否则返回验证错误作为对象。 try that out like below像下面这样试试

 if (this.pensionsInvalidite) {
      for (const pension of this.pensionsInvalidite) {
        if (!pension.dtFin) {
          if (control.value === pension.organisme.code && !this.myDtFin) {
            console.log('yes');
            return null;
          }
        }
      }
    }
    return { organismeValidator: false };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM