在 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/