javascript - $scope 或 $scope.$watch 中的作用域?

标签 javascript angularjs watch

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

真的更好吗?这种方式背后的原因是什么?

最佳答案

来自 AngularJs docs

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/

相关文章:

javascript - jQuery 和 Javascript - 通过对象的属性缓慢循环

javascript - 确保元素始终位于顶部

javascript - 使用新数据和不同的列配置在同一 DOM 元素上重新初始化制表符

javascript - 是否可以触发事件然后将事件扩展到另一个元素?

javascript - 查询 dom 中的 angularjs 对象

javascript - AngularJS '$watch' 值更改时不会触发

javascript - Angularjs Datatable 服务器端分页

angularjs - Interceptor 错误调用 successCallback 后使用 $promise.then()

bash - 使用 bash 和 watch 监控 qemu-kvm

java - 使用 OrbitzWorld 的 consul-client 观察 Consul 中特定键的变化