问题
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