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 - 所有 $watch 执行后的 AngularJS Controller 事件

Javascript和HTML,Javascript函数没有输出

javascript - R - 如何在网站上运行 javascript 按钮以显示所有要抓取的值

javascript - 触发点击 div,这样它就会在 pagedown 和 pageup 上滚动

javascript - 如何将 $scope 传递给 $factory

node.js - Gulp.js : "gulp-chug" only runs one file even when set to watching many

javascript - 使用选择器获取相关项目

javascript - 哪个javascript IDE会自动解析HTML代码中的include js文件?

javascript - Angularjs 在页面加载时显示复选框被选中

xcode - 观看Xcode中的表达式