Check if input control has certain type of vallidator in angular2

前端 未结 2 1497
孤城傲影
孤城傲影 2021-01-18 04:39

I have component that wraps input field. In the component i receive the Control object from @Input() inputControl: Control;. In the template i have

2条回答
  •  难免孤独
    2021-01-18 04:59

    I couldn't get the above to work which isn't surprising given the changes to Angular since Jan. With the latest version of Angular (2.2.0)you will need something like this in your class.

      get required(): boolean { 
        var _validator: any = this.inputControl.validator && this.inputControl.validator(this.inputControl);
        return _validator && _validator.required;
      }
    

    This will also handle the case where you have multiple validators in a reactive form e.g.

          name: ['', [Validators.required, Validators.minLength(2)]]
    

提交回复
热议问题