Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

$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.

like image 673
Ben Jacobs Avatar asked May 01 '26 15:05

Ben Jacobs


1 Answers

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.

like image 75
John Tseng Avatar answered May 03 '26 04:05

John Tseng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!