在家里,我有一个产品列表,点击打开产品模式的按钮,以添加到购物车。添加该金额时,该金额将显示在图标旁边。我希望在app.component中关闭此模式以更新页脚中的图标。我需要用没有刷新就关闭的模态值更新这个购物车。
servico.ts
public totalShop: BehaviorSubject<String>;
constructor() {
this.totalShop = new BehaviorSubject('0');
}
setTotalShop(val) {
this.totalShop.next(val);
}
clearTotalShop() {
this.totalShop.next('0');
}
getTotalShop(){
return this.totalShop.asObservable();
}modalProduto.ts
fnShop(){
quantidade = quantidade +1
this.servico.setTotalShop(quantidade)
}app.component.ts
quantshop;
subscription: Subscription;
ngOnInit(): void {
this.subscription = this.servico.getTotalShop().subscribe(val => this.quantshop = val);
console.log(this.quantshop)
}app.module
providers: [servico]imagem

发布于 2019-11-08 12:14:41
我想你用的是角质材料徽章。
您可以在html中使用角异步管道。
<mat-icon [matBadge]="global.getTotalShop() | async">icon</mat-icon>您需要使服务公开,才能在html中访问它,或者在分配给component.ts的BehaviourSubject中设置另一个变量。
https://stackoverflow.com/questions/58766225
复制相似问题