this vs scope in AngularJS controllers
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In AngularJS controllers, both $scope and this can expose data to the view, but they lead to different code styles. In most non-legacy AngularJS code, this with controllerAs is the cleaner default, while $scope remains useful for a few framework-specific tasks such as watches, events, and integration with older code.
What $scope Does
$scope is the object AngularJS uses for view binding and scope inheritance. In older tutorials, controllers often attach everything directly to it.
That works, but it also means your controller logic is tightly coupled to AngularJS scope behavior, including prototypal inheritance quirks in nested scopes.
What this Does
Inside a controller, this refers to the controller instance. Combined with controllerAs, it gives you a view-model style API that is easier to read and reason about.
Template:
This makes it obvious where each property comes from and avoids the “which scope am I on” confusion common in larger AngularJS templates.
Why controllerAs Became Preferred
The controllerAs style has three big advantages.
First, it reads like normal object-oriented JavaScript. The template refers to vm.name, not an implicit scope chain.
Second, it reduces scope inheritance problems. Nested directives and repeated templates can create child scopes, and direct $scope usage can make property shadowing harder to debug.
Third, it aligns better with the component style that later became standard in modern Angular. Even in old AngularJS codebases, that makes controllers easier to maintain.
When $scope Is Still Useful
$scope did not become obsolete. Some AngularJS APIs still interact with it directly, especially:
- '
$watch' - '
$onand $emit' - manual digest-related integration with non-Angular code
Example:
In other words, use this for public controller state and methods, and keep $scope for the smaller set of framework hooks that genuinely need it.
The this Binding Trap
One reason AngularJS developers often write var vm = this; is JavaScript binding behavior. Inside callbacks, this may no longer refer to the controller unless you preserve it.
Using vm avoids accidental rebinding and keeps the code stable across callbacks.
Common Pitfalls
The biggest mistake is mixing $scope properties and this properties arbitrarily in the same controller. That makes the template and controller contract harder to follow.
Another mistake is using this in callbacks without preserving the reference. In plain JavaScript functions, this can change unexpectedly.
A third mistake is assuming controllerAs removes every need for $scope. It does not. Some AngularJS APIs still depend on it.
Summary
- '
$scopeandthiscan both expose controller data, but they encourage different styles.' - '
thispluscontrollerAsis usually cleaner and easier to maintain.' - '
$scopeis still useful for watches, events, and certain AngularJS-specific hooks.' - Use a stable alias such as
vmto avoidthisbinding problems in callbacks. - Avoid mixing both styles randomly inside the same controller.
Related reading
- Thymeleaf theach filtered with thif
- To Ajaxify Or Not?
- Trigger a button click with JavaScript on the Enter key in a text box
- Trigger callback after getting multiple json files asynchronously
- Trim string in JavaScript
- Trying to implement a SIMPLE promise in Reactjs
- Turning off eslint rule for a specific file
- TypeError brain.NeuralNetwork is not a constructor
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.