简体   繁体   English

@Input之前的角验证器runnng

[英]Angular validators runnng before @Input

I have a component that is actually a form control. 我有一个实际上是窗体控件的组件。 This separate component takes an input (@Input) and is supposed to pass it to a form validator. 此单独的组件接受输入(@Input),并应将其传递给表单验证器。 The problem is that the form validator runs before the input is defined. 问题在于表单验证程序在定义输入之前运行。 So the result is a validator with undefined parameters. 因此,结果是带有未定义参数的验证器。 What is the solution?? 解决办法是什么?? Thanks... 谢谢...

export class InputComponent{

  @Input() min: number; 
  @Input() max: number; 

  formControl: FormControl;

  constructor() {

    this.formControl = new FormControl('', [
      Validators.required,
      CustomValidators.minMaxlength(this.min, this.max)
    ]);

  }

}

In Angular, Component Inputs aren't fully resolved until the ngOnInit lifecycle hook is called. 在Angular中,只有在调用ngOnInit生命周期挂钩之前,才完全解析组件输入。 In components, you generally don't want to have much of anything in the constructor and most initialization should be done in the ngOnInit hook or later. 在组件中,通常不需要构造函数中的任何东西,大多数初始化应在ngOnInit钩子或更高版本中完成。

export class InputComponent{

  @Input() min: number; 
  @Input() max: number; 

  formControl: FormControl;

  constructor() {}


 ngOnInit() {
    this.formControl = new FormControl('', [
      Validators.required,
      CustomValidators.minMaxlength(this.min, this.max)
    ]);

  }
}

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

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