在article中发现了这样一个idea :
Notice how the value function takes the scope as parameter (without the $ in the name). Via this parameter the value function can access the $scope and its variables.
$scope.$watch( function( scope ) {
return scope.val;
...
而不是我以前的做法:
$scope.$watch( function() {
return $scope.val;
...
真的更好吗?这种方式背后的原因是什么?
最佳答案
function(scope): called with current scope as a parameter.
因此它不会改变您的代码的行为。然而,这个版本阻止了在回调中捕获 $scope 变量:
$scope.$watch(function(scope) {
return scope.val;
}, function(value){ });
关于javascript - $scope 或 $scope.$watch 中的作用域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27273898/