There are two patterns in use for accessing controller functions: this
and $scope
.
Which should I use and when? I understand this
is set to the controller and $scope
is an object in the scope chain for views. But with the new “Controller as Var” syntax, you can easily use either. So what I’m asking is what is best and what is the direction for the future?
Example:
-
Using
this
function UserCtrl() { this.bye = function() { alert('....'); }; }
<body ng-controller="UserCtrl as uCtrl"> <button ng-click='uCtrl.bye()'>bye</button>
-
Using
$scope
function UserCtrl($scope) { $scope.bye = function () { alert('....'); }; }
<body ng-controller="UserCtrl"> <button ng-click='bye()'>bye</button>
I personally find the this.name
to be easier on the eye and more natural compared to other Javascript OO patterns.
Advice please?