Angular 2 splice首先在数组中删除

时间:2016-12-12 23:11:16

标签: angular typescript

在Angular 2中使用splice时 - 前端数组会删除第一行中的任何值。后端删除了正确的后端。

deleteCustomer(customer: Customer){
    //opt1 - removes first
    this.customers.splice(this.customers.indexOf(customer),1);
    //opt2 - this works although it will loop all records
    this.customers.forEach((t, i) => { 
        if (t.customer_id === customer.customer_id) { 
          this.customers.splice(i, 1); }
     });
    //the backend deletes the correct record in both options
    return this.http.delete(`${this.customersUrl}/${customer['customer_id']}`)
    .subscribe(
        data => console.log(data),
        error => console.error(error)
    );
}

最好我想使用option1 - 我在这里缺少什么?

在我的表格的一部分下面:

      <div class="widget-body">
        <form class="form-horizontal" role="form" [formGroup]="customerForm" (ngSubmit)="onSubmit()">
          <fieldset>
            <legend><strong>Customer</strong> form</legend>
            <div class="form-group row">
              <label for="customer_id-field" class="col-md-4 col-form-label text-md-right">
                Id
              </label>
              <div class="col-md-7">
                <input type="text" formControlName="customer_id"  id="customer_id-field" class="form-control" disabled>
              </div>
            </div>            

0 个答案:

没有答案