Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access declared variable from app.component to other components?

I am new to Typescript and Angular 2. I tried to look for an answer in the web but it seems they don't work for me.

Say I have an app.component as shown below:

export class AppComponent{
    cartPayableAmount = 0;

    ........
}

in my app.component.html, the cartPayableAmount is used here

<div> {{cartPayableAmount}} </div>

Now I want to access the cartPayableAmount entity in other class as shown below: So when ever I envoke TestHere() the value for cartPayableAmount will change and it will reflect at the app.component.html.

export class BuyTestComponent {
    TestHere() {
        // should update cart PayableAmount here.....
        cartPayableAmount  = "Some value"
    }
}

Please help. Thanks

like image 807
wal Avatar asked Dec 07 '25 08:12

wal


1 Answers

You can create a new service, maybe called ShoppingCart, that holds that value. And then it can be injected into your components.

But in your example, cartPayableAmount is a computed value, so you'll want it to be a method or Observable that computes it instead of a class property.

like image 131
DerekMT12 Avatar answered Dec 08 '25 22:12

DerekMT12



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!