vineet
vineet

Reputation: 14266

$rootscope watch doesn't work for scond time

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

Answers (2)

Arno_Geismar
Arno_Geismar

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 :

You dont need watchers

Upvotes: 1

user5210690
user5210690

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

Related Questions