带参数的Angular 2表单Change事件

时间:2018-08-24 10:12:48

标签: angular angular2-forms

下面是用于将文件添加为表单数据的HTML。我想删除“添加”按钮并在更改$ event期间传递值(ticket.id),这将触发组件写入文件。

可以做到吗?

<tr *ngFor='let ticket of tickets'>
  <td>
    <form [formGroup]="form" (ngSubmit)="onSubmit()">
      <div class="form-group">
        <input 
           type="file" 
           class="custom-file-input" 
           id="avatar" (change)="onFileChange($event)" #avatar>
      </div>
      <button 
           type="button" 
           (click)="onSubmit(ticket.id)" 
           [disabled]="form.invalid || loading" 
           class="btn btn-success">
           Add File<i class="fa fa-spinner fa-spin fa-fw" *ngIf="loading"></i>
      </button>
    </form>
  </td>
</tr>

1 个答案:

答案 0 :(得分:0)

我很确定,您应该能够将ticket.id作为第二个参数传递给change事件,如下所示:

<tr *ngFor='let ticket of tickets'>
  <td>
    <form [formGroup]="form" (ngSubmit)="onSubmit()">
      <div class="form-group">
        <input 
           type="file" 
           class="custom-file-input" 
           id="avatar" 
           (change)="onFileChange($event, ticket.id)" 
           #avatar>
      </div>
    </form>
  </td>
</tr>