user2227400
user2227400

Reputation:

$scope.watch ('$scope',.. in Angular

I want to check for fun every changes in the scope and I've tried the following:

$scope.$watch('$scope', function(newValue) {
    console.log ("$scope's NewValue", newValue);
}, true);

I get the log message:

$scope's NewValue undefined

Is that, what am trying, is possible anyway?

Upvotes: 1

Views: 706

Answers (1)

Deblaton Jean-Philippe
Deblaton Jean-Philippe

Reputation: 11397

I really suggest you to not do this. It will have a high cost in your application.

$scope.$watch(function() { // First function must return the object that you want to watch.
    return $scope;      
},
function(newValue, oldValue) { // This is a callback executed everytime your $scope will change
    console.log("$scope's NewValue : ", newValue);   // You should rather use $log if you want to unit test your code one day
},
true   // Needed to tell angular to watch deeply inside $scope (every sub item).
);

Upvotes: 1

Related Questions