简体   繁体   English

在`$ scope`或`this`上定义的控制器函数之间的性能差异 - AngulrJS

[英]Performance differences between controller functions defined on `$scope` or `this` - AngulrJS

In Angular, you can define methods in your controller by attaching them to $scope : 在Angular中,您可以通过将它们附加到$scope来定义控制器中的方法:

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

Of course, you can also attach them to this , which I've seen used for communicating between directives and a parent controller: 当然,您也可以将它们附加this ,我已经看到它用于指令和父控制器之间的通信:

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

Are there performance differences between the two approaches due to Angular watching the values? 由于Angular观察值,这两种方法之间是否存在性能差异?

Even if there aren't performance differences, it seems like a nice way of keeping some methods private, so they won't accidentally be accessed from the View. 即使没有性能差异,它似乎是保持某些方法私有的好方法,因此不会意外地从View访问它们。

From the docs ( http://docs.angularjs.org/guide/dev_guide.mvc.understanding_controller ): 来自docs( 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. 注意:早期版本的Angular(1.0 RC之前版本)允许您与$ scope方法互换使用,但现在不再是这种情况了。 Inside of methods defined on the scope this and $scope are interchangeable (angular sets this to $scope), but not otherwise inside your controller constructor. 在范围内定义的方法内部,此范围和$ scope是可互换的(角度设置为$ scope),但在控制器构造函数中则不然。

So this is $scope , but not for long. 所以this$scope ,但不是很长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM