如何在* ngFor循环中隐藏div

时间:2019-03-15 11:35:28

标签: angular typescript angular-material angular6

我想在* ngFor循环内添加一个表单。以下是我重复按钮的尝试,例如:What I want to acheive...

html文件:

<div *ngFor="let passenger of passengerForm;let i=index;">
  <form>
    <mat-form-field>
      <input matInput type="text" placeholder="Enter Name" 
        [(ngModel)]="passenger.Name">
    </mat-form-field> 

  </Form>
  <div>
    <button (click)="addFieldValue(i); ">
      Add Passenger
    </button>
  </div>

</div>

打字稿文件:

newAttribute ={}
addFieldValue(index)  {

  if ( index < 3) {
    this.passengerForm.push(this.newAttribute )
    this.newAttribute = {};

  }
}

1 个答案:

答案 0 :(得分:1)

<div *ngFor="let passenger of passengerForm;let i=index;">
   <input matInput type="text" placeholder="Enter Name" [(ngModel)]="passenger.Name">
</div>

尝试这种方式。您只需要包装输入字段即可。