1

I try the suggestion in this question: Angular 2 template driven form with ngFor inputs

<div ngForm  #form="ngForm">
      <div *ngFor="let item of reqLine">
        <div [ngClass]="{'has-error': qtyInput.hasError('pattern') && qtyInput.touched}">
          <input name="product-{{item.rowNum}}"
                 [(ngModel)]="item.itemNum"  [pattern]="[0-9]{10}(\/[0-9])"
                 #qtyInput="ngModel">
                 {{qtyInput.valid}}
          <button  [disabled]="!qtyInput.valid">Add to cart</button>
      </div>
     </div>
      <button [disabled]="!form.valid">Add all</button>
</div>

but it doesn't work for me.

when one of the rows is not valid all of the rows becomes not valid.

I want to access the form from ts file. I declared is at like this:

@ViewChild('form') form:NgForm;

IsValid():boolean{
if(!this.form.valid){
  return false;
}

where I am wrong?

thanks.

0 Answers0