Angular2-删除后更新UI

usm*_*ana 4 typescript angular2-template angular2-routing angular

我有一个angular2应用程序,其后端是在java中.我有一份客户名单.当我单击以删除客户时,客户将被删除,但列表不会更新.如果我手动刷新页面,则列表会更新.我尝试在delete方法的订阅中路由到列表组件,但这不起作用.

列表customers.component.html

<tr [class.warning]="customer.isDefault == 1" *ngFor="let customer of customers | orderBy:['firstName'] | search:searchCustomer.value;let serial = index">
                <td>{{ serial+1 }}</td>
                <td>{{ customer?.firstName+' '+customer?.lastName}}</td>
                <td>{{ customer.email}}</td>
                <td>{{ customer.mobileNumber}}</td>
                <td>{{ customer.streetAddress}}</td>
                <td>{{ customer.priceList?.name}}</td>
                <td><a [routerLink]="['/loggedIn','customer','edit', customer.id ]"><i class="fa fa-edit fa-2x"></i></a></td>
                <td><a (click)="delete(customer)"><i class="fa fa-trash-o fa-2x"></i></a></td>
              </tr>
Run Code Online (Sandbox Code Playgroud)

列表customers.component.ts

    ngOnInit()
    {
        this.refreshCustomersList();
    }

    delete(customer)
    {
        this.userService.delete(customer.id)
            .subscribe(
                success=>
                {
                    var index = this.customers.indexOf(customer, 0);
                    if (index > -1)
                    {
                        this.customers.splice(index, 1);
                    }
                }
            )

    }

    refreshCustomersList()
    {
        this._authHttp.get(
                this.appService.getApiUrl() + "api/customer/list"
            )
            .map(res=>res.json())
            .subscribe(
                successResponse=>
                {
                    this.customers = successResponse.data.customers;
                },
                () => console.log("Request Completed")
            )

    }
}
Run Code Online (Sandbox Code Playgroud)

jhh*_*f02 6

尝试调用this.refreshCustomersList();你的delete函数,如下所示:

    delete(customer)
{
    this.userService.delete(customer.id)
        .subscribe(
            success=>
            {
                var index = this.customers.indexOf(customer, 0);
                if (index > -1)
                {
                    this.customers.splice(index, 1);
                    this.refreshCustomersList();
                }
            }
        )

}
Run Code Online (Sandbox Code Playgroud)

这将customers在删除客户后更新阵列.