Ben Jacobs
Ben Jacobs

Reputation: 2526

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.

Upvotes: 3

Views: 217

Answers (1)

John Tseng
John Tseng

Reputation: 6352

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.

Upvotes: 1

Related Questions