LiverpoolOwen
LiverpoolOwen

Reputation: 816

How to use Angular validation properties inside for?

I am trying to find out if the input element in the iteration of a for(*ngFor) loop iteration is dirty but I cannot find a way to dynamically name the validation variable, therefore cannot get the outcome I am looking for.

<tr *ngFor="let user of users">
  <td>
    <div class="row">
      <div class="col-sm-10">
         <input id="{{user.id}}" class="form-control" [(ngModel)]="user.name" #[user.id]="ngModel" />
      </div>
      <div class="col-sm-2">
         <button *ngIf="[user.id].dirty" class="btn btn-success">Save</button>
      </div>
    </div>
  </td>
</tr>

Upvotes: 0

Views: 107

Answers (1)

J&#225;n Halaša
J&#225;n Halaša

Reputation: 8421

Validation properties can be found on AbstractControl instances. So you need to mark your form tag and then get a particular control from its controls property:

<form #myFrom="ngForm">
    <input name="myInput{{user.id}}">
    {{ myForm.controls['myInput' + user.id]?.dirty }}
</form>

Upvotes: 1

Related Questions