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

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

为了好玩,我想检查作用域中的每一个变化,我尝试了以下方法:

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

我收到日志消息:

$scope's NewValue undefined

不管怎样,我正在尝试的东西是可能的吗?

我真的建议你不要这样做。它会在您的应用程序中产生很高的成本。

$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).
);