javascript - 在 `$scope` 或 `this` 上定义的 Controller 函数之间的性能差异 - AngularJS

标签 javascript angularjs scope

在 Angular 中,您可以通过将方法附加到 $scope 来在 Controller 中定义方法:

$scope.myFunction = function () { ... }

当然,您也可以将它们附加到 this,我看到它用于在指令和父 Controller 之间进行通信:

/* within the controller */
this.myFunction = function () { ... }

由于 Angular 观察值,这两种方法之间是否存在性能差异?

即使没有性能差异,这似乎也是一种保持某些方法私有(private)的好方法,因此它们不会意外地从 View 中访问。

最佳答案

来自文档(http://docs.angularjs.org/guide/dev_guide.mvc.understanding_controller):

NB: Previous versions of Angular (pre 1.0 RC) allowed you to use this interchangeably with the $scope method, but this is no longer the case. Inside of methods defined on the scope this and $scope are interchangeable (angular sets this to $scope), but not otherwise inside your controller constructor.

所以 this$scope,但时间不长。

关于javascript - 在 `$scope` 或 `this` 上定义的 Controller 函数之间的性能差异 - AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18254869/

相关文章:

javascript - angularjs ngRepeat 有时我无法向下滚动

javascript - 无法通过 JavaScript 访问变量 - 范围错误?

angularjs - 使用 ng-repeat 和 inline-block div 的奇数对齐/边距/间距

javascript - 如何在 AngularJS 指令中绑定(bind)属性?

python - 本地/全局范围

perl - Perl subs 没有词法作用域是一个设计缺陷吗?

javascript - 如何在 ASP.NET MVC 应用程序中自动压缩和最小化 JavaScript 文件?

javascript - 在knockout.js中,是否可以使用动态绑定(bind)值?

javascript - 将 jQuery Mobile 应用程序连接到服务器数据库并在互联网连接关闭时打开弹出窗口

javascript - 如何处理(好吧,模拟)触摸设备上的悬停行为?