how I can get Validators.minLength of form input and set it in html dynamicly?

Viewed 55

I have a form in angular and I want to use value of form "Validators.minLength" in html input tag.

I want Know is any way to use directly from form validator in html;

sample: [maxLength]="form.get('zipCode').validators('maxLength)"

My ts code is:

            zipCode: [ '', [
                Validators.required,
                Validators.maxLength(10),
                number,
            ],

and html is:

                        <input type="tel"
                               formControlName="zipCode"
                               [maxLength]="10"  // here I want insert dynamic number from ts file
                               placeholder="insert your zip code">

tanks for your helping.

2 Answers

TS

this.maxLength=10;
zipCode: [ '', [
                Validators.required,
                Validators.maxLength(this.maxLength),
                number,
            ],

HTML

<input type="tel" formControlName="zipCode" placeholder="insert your zip code">

TS:

public maxZipCodeLength: number = 10;

HTML:

<input type="tel" formControlName="zipCode" [maxlength]="maxZipCodeLength" />

This should be enough.

Related