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

左心房为你撑大大i 提交于 2019-12-22 10:26:32

问题


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.


回答1:


From the 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. Inside of methods defined on the scope this and $scope are interchangeable (angular sets this to $scope), but not otherwise inside your controller constructor.

So this is $scope, but not for long.



来源:https://stackoverflow.com/questions/18254869/performance-differences-between-controller-functions-defined-on-scope-or-thi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!