在`$ scope`或`this`定义控制器功能的性能差异 - AngulrJS [英] Performance differences between controller functions defined on `$scope` or `this` - AngulrJS

查看:92
本文介绍了在`$ scope`或`this`定义控制器功能的性能差异 - AngulrJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在角,您可以通过它们连接到 $范围定义你的控制器方法

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.

推荐答案

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

注:$ P $角的pvious版本(pre 1.0 RC)允许您使用$范围的方法交替使用,但这已不再是这种情况。内部对这一范围和$范围定义的方法是可以互换的(角度将其设置为$范围内),而不是其他控制器的构造函数中。

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.

所以这个 $范围,但时间不长。

这篇关于在`$ scope`或`this`定义控制器功能的性能差异 - AngulrJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆