snapjay / ngCart

Really simple shopping cart for AngularJS
http://ngcart.snapjay.com
380 stars 236 forks source link

How to access ngCart.getTotalItems and ngCart.totalCost in my controller's view #97

Open Jesus82 opened 7 years ago

Jesus82 commented 7 years ago

Hi, I would need to access ngCart.getTotalItems() and ngCart.totalCost() functions from my controller's view (in order to display some items conditionally based on the number of items added to the cart and the total cost of it). What would be the way to correctly accessing those functions? Should I create a service/factory for it?

Thanks in advance

GaboDot commented 7 years ago

Hi! For total items I used: ngCart.getTotalItems() and for total cost just called: ngCart.totalCost() | currency

Another way could be: var cart_items = ngCart.getCart().items; var total = ngCart.getCart().totalCost;

Hope this helps!

Jesus82 commented 7 years ago

Hi, thanks for your reply. I have also used those functions when I'm inside the ng directive (for example, in the cart.html or checkout html files). The point is that I would like to use it also from outside the directive (that is, from my controller's view, but outside the parts injected by <ng-cart></ng-cart> or <ng-checkout></ng-checkout>

Did you manage to do that with the ngCart object? I don't seem to be able to access the object outside the directive, but maybe I'm doing something wrong...