Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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 this.name to be easier on the eye and more natural compared to other Javascript OO patterns.

Advice, please?

1 Answer

0 votes
by (106k points)

Actually, the $scope is being removed in Angular 2.0. Thus, using this would be an approach others want to follow as the date of release of Angular 2.0 comes closer.

Related questions

0 votes
1 answer
asked Aug 27, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...