angularjs - Parent Controller listen to event from child controller -
i have parent shell html file has icon update number of products in cart. number supposed retrieved factory.
angular .module('awesome') .factory('productfactory', productfactory); function productfactory($log) { var factory = { addcartproducts: addcartproducts, getcartcount: getcartcount }, cartproducts = []; return factory; function addcartproducts(product) { cartproducts.push(product); getcartcount(); $log.info(cartproducts.length); } function getcartcount() { return cartproducts.length; } }
in parent controller have scope
value being pulled
$scope.count = productfactory.getcartcount()
in child controller i'm adding cart
$scope.addtocart = function (product) { productfactory.addcartproducts(product); };
however when add cart, can see in log being added, however, item in parent controller not update. missing?
the reason why count not updating because set value returned productfactory.getcartcount(). if make further changes $scope.count in controller, updates occur, further changes productfactory.getcartcount() won't. here do:
parent controller:
$scope.getcount() = function () { return productfactory.getcartcount(); }
and in view, instead of using {{count}}, use {{getcount()}}
i hope helps!
Comments
Post a Comment