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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.