Reputation: 14266
I am very new in Angularjs. I defined $rootscope watcher in controller. First time(during page load) it will work means alert display but after changes in $rootScope.cartItems
it doesn't work.
eshopApp.controller('cartCtrl', function ($scope, $rootScope, cartService, $cookies) {
$rootScope.$watch('cartItems', function (newVal, oldVal) {
cartService.cartSubTotal(function (data) {
$scope.cartSubTotalVal = data;
alert('total '+$scope.cartSubTotalVal);
});
});
});
Upvotes: 0
Views: 74
Reputation: 2330
On an entirely unrelated note: Don't use $rootScope watchers or watchers in general. Only a really small part of use cases actually requires watchers : Check out this article for more information :
Upvotes: 1
Reputation:
pass true
in third parameter. e.g
$rootScope.$watch('cartItems', function (newVal, oldVal) {
cartService.cartSubTotal(function (data) {
$scope.cartSubTotalVal = data;
alert('total '+$scope.cartSubTotalVal);
});
});
},true);
Upvotes: 2