Prajeet Shrestha
Prajeet Shrestha

Reputation: 8108

How to add dynamic formControlName in AngularJs2

I am making forms through form builder in angularjs2, I want to add formControlName property/attribute of form element like this:

   <input type="text" formControlName={{'client_name' + i}} placeholder="Required" />

How can I do this?

Edit:

I also want to put the validity message beside input element like this: How do I make the client_name inside the ngIf condition block dynamic as well?

<div *ngIf=“! userform.controls['client_name'].valid”>
  Client Name is Not Valid
</div>

It should have same value as {{'client_name' + i}}

Upvotes: 1

Views: 1310

Answers (1)

Sky.Li
Sky.Li

Reputation: 517

Please use[formControlName]='client_name + i' instead of formControlName={{'client_name' + i}}

And use userform.controls[client_name + i].valid instead of userform.controls['client_name'].valid

For More Information: https://angular.io/guide/dynamic-form

Upvotes: 2

Related Questions