In this video Iftach Bar shows how to avoid some common bugs using the controllerAs syntax in Angular.
- [Instructor] In the previous video…we saw the issue of using a variable in a son scope,…and the problem it might cause…when changing the variable in the son scope.…The problem was caused because of…the way prototype inheritance works with scopes.…To solve this issue, there is a best practice.…We can use an object on the scope,…and always access variables on top of that object.…It is very common in Angular…to use the controller itself for such an object,…and then bind the controller to the scope.…
Let's go to the app.js file, to the playground controller,…and we'll add a line here…at line 4 saying…var vm = this;…This variable vm is the object that we're going to use…in order to store all the other variables we need…on top of that specific scope.…The next line I'm going to write…is to bind this vm to our scope…using $scope.playground, for example, equals vm.…
In the rest of this controller's code,…I'm not going to bind anything else to the scope.…All the other variables and functions…I used to bind to scope,…I'm going to bind to this vm object.…
This advanced AngularJS course helps you explore the connected worlds of $scope and the digest cycle. Instructor Iftach Bar explains scope and inheritance, goes behind the scenes of the digest loop, and teaches both manual and automatic data binding approaches. Plus, learn how to integrate external libraries, diagnose performance issues, and debug your applications.
- $scope and controllers
- $scope built-in variables and methods
- $scope prototype inheritance
- Manual data binding with $scope.$watch
- The digest loop
- Get the $scope in the console
- Forcing $digest
Skill Level Advanced
AngularJS 1: Building a Data-Driven Appwith Ray Villalobos2h 15m Intermediate
1. Scope Basics
2. Manual Data Bindings
3. The Digest Loop
4. Tips for Debugging Angular in the Console
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.