问题描述
在角,您可以通过它们连接到 $范围
定义你的控制器方法
In Angular, you can define methods in your controller by attaching them to $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:
/* within the controller */
this.myFunction = function () { ... }
是否有因角度看值两种方法之间的性能差异?
Are there performance differences between the two approaches due to Angular watching the values?
即使有没有性能上的差异,似乎保留了一些方法私人的一个很好的方式,所以他们不会意外地从视图访问。
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.
推荐答案
从文档():
注:$ P $角的pvious版本(pre 1.0 RC)允许您使用$范围的方法交替使用,但这已不再是这种情况。内部对这一范围和$范围定义的方法是可以互换的(角度将其设置为$范围内),而不是其他控制器的构造函数中。
所以这个
是 $范围
,但时间不长。
这篇关于在`$ scope`或`this`定义控制器功能的性能差异 - AngulrJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!