Angular 角度2-更改后调用函数

Angular 角度2-更改后调用函数,angular,service,Angular,Service,我想在addToCart(cartItem)将项目添加到购物车后调用getCartSum()函数,以便接收购物车的新“总值” 这是我的服务: public cartItems: any[] = []; public total: number; constructor() {} addToCart(cartItem) { this.cartItems.push(cartItem); } getCart() { return this.cartItems; } getCar

我想在
addToCart(cartItem)
将项目添加到购物车后调用
getCartSum()
函数,以便接收购物车的新“总值”

这是我的服务:

public cartItems: any[] = [];
public total: number;

constructor() {}

addToCart(cartItem) {
    this.cartItems.push(cartItem);
}

getCart() {
    return this.cartItems;
}

getCartSum() {
    this.total = 0;
    if (Object.keys(this.cartItems).length != 0) {
        for (var x of this.cartItems) {
            this.total += x.product.price;
        }
        return this.total;  
    }
    return this.total;
}
这里是我的ShoppingCart组件:

export class ShoppingCart  {
public title: string = "ShoppingCart";
public cartItems: any[];
public total: number;

constructor(private _cartService: CartService) {
    this.cartItems = this._cartService.getCart()
    this.total = this._cartService.getCartSum()
}

getCartSum() {
    this.total = this._cartService.getCartSum()
}
}
addToCart() {
    this.total = this._cartService.addToCart()
}
你是说这个吗?:
服务:

addToCart(cartItem) {
    this.cartItems.push(cartItem);
    return this.getCartSum();
}
组成部分:

export class ShoppingCart  {
public title: string = "ShoppingCart";
public cartItems: any[];
public total: number;

constructor(private _cartService: CartService) {
    this.cartItems = this._cartService.getCart()
    this.total = this._cartService.getCartSum()
}

getCartSum() {
    this.total = this._cartService.getCartSum()
}
}
addToCart() {
    this.total = this._cartService.addToCart()
}
你是说这个吗?:
服务:

addToCart(cartItem) {
    this.cartItems.push(cartItem);
    return this.getCartSum();
}
组成部分:

export class ShoppingCart  {
public title: string = "ShoppingCart";
public cartItems: any[];
public total: number;

constructor(private _cartService: CartService) {
    this.cartItems = this._cartService.getCart()
    this.total = this._cartService.getCartSum()
}

getCartSum() {
    this.total = this._cartService.getCartSum()
}
}
addToCart() {
    this.total = this._cartService.addToCart()
}
在您的服务中创建一个:

private\u totalChangeSource=新行为主体(0);
totalChange$=此项。_totalChangeSource.asObservable();
让您的组件订阅observable,以便收到更改通知:

ngOnInit(){
this.subscription=this.\u cartService.totalChange$.subscription(
newTotal=>this.total=newTotal);
}
恩贡德斯特罗(){
//防止组件损坏时内存泄漏
this.subscription.unsubscripte();
}
添加项时,在BehaviorSubject上发出事件:

addToCart(cartItem){
this.cartItems.push(cartItem);
this.updateTotal();
}
updateTotal(){
这个总数=0;
if(Object.keys(this.cartItems).length!=0){
for(此.cartItems的变量x){
此项合计+=x产品价格;
}
}
this.\u totalChangeSource.next(this.total);
}
在您的服务中创建一个:

private\u totalChangeSource=新行为主体(0);
totalChange$=此项。_totalChangeSource.asObservable();
让您的组件订阅observable,以便收到更改通知:

ngOnInit(){
this.subscription=this.\u cartService.totalChange$.subscription(
newTotal=>this.total=newTotal);
}
恩贡德斯特罗(){
//防止组件损坏时内存泄漏
this.subscription.unsubscripte();
}
添加项时,在BehaviorSubject上发出事件:

addToCart(cartItem){
this.cartItems.push(cartItem);
this.updateTotal();
}
updateTotal(){
这个总数=0;
if(Object.keys(this.cartItems).length!=0){
for(此.cartItems的变量x){
此项合计+=x产品价格;
}
}
this.\u totalChangeSource.next(this.total);
}

您可能希望将
\u cartService
变量存储在
ShoppingCart
的构造函数中,以便在
getCartSum
方法中可以访问该变量?嗯,我希望在将项目添加到购物车时,“total”值将自动更新。目前,我必须手动调用服务中的方法来接收新值。您可能希望将
\u cartService
变量存储在
ShoppingCart
的构造函数中,以将其存储到本地成员属性中,以便可以在
getCartSum
方法中访问它?嗯,我希望将项目添加到购物车时,值将自动更新。目前,我必须手动调用服务中的方法来接收新值。thx为您解答,这项工作!我还解决了我的问题:
{{{u cartService.getCartSum()| currency:'EUR':true:'1.2-2'}}}
或者这不是一个好的解决方案???@larz,在模板中使用
getCartSum()
效率较低,因为每次运行更改检测时都会执行该方法。使用可观察值,仅当购物车内容发生变化时才计算总数。thx对于您的回答,这项工作的!我还解决了我的问题:
{{{u cartService.getCartSum()| currency:'EUR':true:'1.2-2'}}}
或者这不是一个好的解决方案???@larz,在模板中使用
getCartSum()
效率较低,因为每次运行更改检测时都会执行该方法。使用可观察值,仅当购物车内容发生变化时,才计算总数。