英文:
How to refresh the Parent component after a button on child component is pressed? - Angular
问题
以下是翻译好的部分:
我目前正在更新弹出窗口中的配料数量,我希望在弹出窗口关闭时重新渲染配料卡并显示新的数量,但我不确定如何实现这一点。
这是我的配料服务代码:
//UPDATE
updateAmount(id: string, newAmount: number): Observable<Ingrediant>{
return this.http.put<Ingrediant>(`${this.url}/${id}`, {amount: newAmount});
}
这是配料组件的HTML:
<div class="ingrediant-slider">
<hr>
<h4 class="ingrediant-heading">Bread</h4>
<div class="carousel">
<img src="../../assets/arrows/left-arrow.png" class="arrow-left" (click)="scroll(-scrollAmount)">
<img src="../../assets/arrows/right-arrow.png" class="arrow-right" (click)="scroll(scrollAmount)">
<div class="card" *ngFor="let breadIngrediants of breadIngrediants; let i=index">
<div class="top-circle">
<h4>{{breadIngrediants.amount}}</h4>
</div>
<div class="circle">
<img [src]="breadIngrediants.image" class="card-img" alt="Image of burger">
</div>
<h3>{{breadIngrediants.name}}</h3>
<button (click)="showPopup = true; selectedIngrediants = breadIngrediants">Update</button>
</div>
</div>
</div>
<app-popup *ngIf="showPopup" [selectedIngrediants]="selectedIngrediants" (close)="showPopup = false" (updated)="onUpdate()"></app-popup>
这是配料的.ts文件:
// 将此方法添加到在事件被触发时更新配料列表
onUpdate() {
// 重新获取配料数据并更新本地变量
this.ingrediantService.items$.subscribe((data) => {
this.breadIngrediants = data;
console.log(data);
this.cdr.detectChanges();
});
}
这是弹出窗口的HTML:
<div class="popup" [style.top.px]="getYPosition(selectedIngrediants)">
<div class="left-pannel">
<div class="circle">
<img [src]="selectedIngrediants.image" class="card-img" alt="Image of burger">
</div>
</div>
<div class="right-pannel"></div>
<h2>{{ selectedIngrediants.name }}</h2>
<p>{{ getDescription() }}</p>
<h4>Amount</h4>
<input type="number" matInput placeholder="0" color="accent" [value]="selectedIngrediants.amount" (change)="detectAmountChange($event)">
<button class="primary-btn" (click)="updateAmount(selectedIngrediants._id!); closePopup();">Update</button>
<button class="secondary-btn" (click)="closePopup()">Cancel</button>
</div>
这是弹出窗口的.ts文件:
// 将newAmountPlaceholder更改为当前输入值
detectAmountChange(e: any){
this.newAmountPlaceholder = +e.target.value
}
@Output() updated: EventEmitter<void> = new EventEmitter<void>();
updateAmount(id: string) {
this.ingrediantService.updateAmount(id, this.newAmountPlaceholder).subscribe((selectedIngrediant) => {
console.log(selectedIngrediant.amount);
this.updated.emit(); // 在更新成功时发出事件
});
}
英文:
so I am currently updating an ingredient amount in a pop-up and I want the ingredient card to rerender and show the new amount when the pop-up closes, however, I am unsure of how to achieve this.
This is my ingredient service code:
//UPDATE
updateAmount(id: string, newAmount: number): Observable<Ingrediant>{
return this.http.put<Ingrediant>(`${this.url}/${id}`, {amount: newAmount});
}
This is the ingredient component HTML:
<div class="ingrediant-slider">
<hr/>
<h4 class="ingrediant-heading">Bread</h4>
<div class="carousel">
<img src="../../assets/arrows/left-arrow.png" class="arrow-left" (click)="scroll(-scrollAmount)">
<img src="../../assets/arrows/right-arrow.png" class="arrow-right" (click)="scroll(scrollAmount)">
<div class="card" *ngFor="let breadIngrediants of breadIngrediants; let i=index" >
<div class="top-circle">
<h4>{{breadIngrediants.amount}}</h4>
</div>
<div class="circle">
<img [src]="breadIngrediants.image" class="card-img" alt="Image of burger"/>
</div>
<h3>{{breadIngrediants.name}}</h3>
<button (click)="showPopup = true; selectedIngrediants = breadIngrediants">Update</button>
</div>
</div>
</div>
<app-popup *ngIf="showPopup" [selectedIngrediants]="selectedIngrediants" (close)="showPopup = false" (updated)="onUpdate()"></app-popup>
This is the ingrediant .ts:
// add this method to update the ingredient list when the event is emitted
onUpdate() {
// re-fetch the ingredient data and update the local variable
this.ingrediantService.items$.subscribe((data) => {
this.breadIngrediants = data;
console.log(data);
this.cdr.detectChanges();
});
}
This is the pop-up HTML:
<div class="popup"[style.top.px]="getYPosition(selectedIngrediants)">
<div class="left-pannel">
<div class="circle">
<img [src]="selectedIngrediants.image" class="card-img" alt="Image of burger"/>
</div>
</div>
<div class="right-pannel">
</div>
<h2>{{ selectedIngrediants.name }}</h2>
<p>{{ getDescription() }}</p>
<h4>Amount</h4>
<input type="number" matInput placeholder="0" color="accent"
[value]="selectedIngrediants.amount"
(change)="detectAmountChange($event)"
>
<button class="primary-btn" (click)="updateAmount(selectedIngrediants._id!); closePopup();">Update</button>
<button class="secondary-btn" (click)="closePopup()" >Cancel</button>
</div>
and the pop-up ts:
//changes newAmountPlaceholder to current input value
detectAmountChange(e: any){
this.newAmountPlaceholder = +e.target.value
}
@Output() updated: EventEmitter<void> = new EventEmitter<void>();
updateAmount(id: string) {
this.ingrediantService.updateAmount(id, this.newAmountPlaceholder).subscribe((selectedIngrediant) => {
console.log(selectedIngrediant.amount);
this.updated.emit(); // emit the event when the update is successful
});
}
答案1
得分: 1
以下是翻译好的内容:
你的代码应该可以工作,尽管它并不是一种很干净的方法。有一点,我猜测弹出窗口在Http请求结束之前被关闭,所以(update)
从未被调用。
我建议的是让父组件处理Http请求,而将弹出窗口仅处理输入。
在pop-up.ts文件中:
@Output() updated: EventEmitter<any> = new EventEmitter<any>();
updateAmount(id: string) {
this.updated.emit({ id: id, amount: this.newAmountPlaceholder });
}
在ingrediant.ts文件中:
onUpdate(popupData: any) {
this.ingrediantService.updateAmount(popupData.id, popupData.amount).subscribe(() => {
//保存后刷新
this.ingrediantService.items$.subscribe((data) => {
this.breadIngrediants = data;
console.log(data);
this.cdr.detectChanges();
});
});
}
最后,在ingredient.html文件中更新HTML:
<app-popup *ngIf="showPopup" [selectedIngrediants]="selectedIngrediants" (close)="showPopup = false" (updated)="onUpdate($event)"></app-popup>
希望这有帮助
英文:
Your code should work, even though it's not a very clean approach. One thing, I guess the popup is being closed before the end of the Http request so the (update)
is never called.
What i recommand is letting the parent component handle the Http Request and leave the Popup to only deal with input.
In pop-up.ts
@Output() updated: EventEmitter<any> = new EventEmitter<any>();
updateAmount(id: string) {
this.updated.emit({id: id, amount: this.newAmountPlaceholder});
}
In ingrediant.ts:
onUpdate(popupData:any) {
this.ingrediantService.updateAmount(popupData.id, popupData.amount).subscribe(() => {
//after saving refresh
this.ingrediantService.items$.subscribe((data) => {
this.breadIngrediants = data;
console.log(data);
this.cdr.detectChanges();
});
});
}
and finally update the html in ingerdiant
<app-popup *ngIf="showPopup" [selectedIngrediants]="selectedIngrediants" (close)="showPopup = false" (updated)="onUpdate($event)"></app-popup>
Hope this works
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论