2017-08-12 220 views
3

在restaurant.component.ts刪除項目

我想從arrayList刪除一個項目並對其進行更新後,刪除後如何更新HTML5表格。查詢正在執行刪除操作,但列表沒有更新。我打電話ngOnInit()方法,但什麼也沒發生

ngOnInit() { 
this.restaurantesService.GetRestaurantes() 
.subscribe((restaurantes => { 
    this.restaurantes = restaurantes; 
})) 

}

qDeleteRestaurantes(CODIGO: number){ 
var restaurantes = this.restaurantes; 
this.restaurantesService.DeleteRestaurantes(CODIGO) 
.subscribe((data) => { if (data.n ==1){ 
    for(let i=0;i<restaurantes.length;i++){ 
    if(restaurantes[i].CODIGO == CODIGO){ 
     this.restaurantes.splice(i,1); 
     this.ngOnInit(); 

    }   
    } 
}}) 

}

+0

你確定你是地球的'if'讓'ngOnInit'被再次調用。同樣作爲註釋,不要調用'ngOnInit',而應該再次調用請求來獲取餐廳。此外,如果你想在此之後再次購買餐館,你爲什麼要做'拼接'。由於您再次獲取餐廳,「拼接」是多餘的。 – Alex

回答

0

在你restaurant.service,聲明一個變量,

restaurants: restaurants[]; 

在您qDeleteRestaurant (CODIGO:number),轉讓

this._restaurantService.restaurants = this.restaurants; 

當然,在構造函數中,聲明

private _restaurantService: RestaurantService,.. 

**你需要實現DoCheck,

ngDoCheck(){ 
    this.restaurants = this._restaurantService.restaurants; 
} 

這個方法是我在我的購物車刪除使用,添加,更新。祝你好運。

enter image description here

+0

您是否嘗試過實施ngDoCheck?祝你今天愉快。 –