This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

JavaScript Function Scope and $scope

6:57 Angular 1.x lesson by

How does AngularJS $scope relate to JavaScript function scope?

Get the Code Now
click to level up comment guidelines


How does AngularJS $scope relate to JavaScript function scope?


Hey Brett,
Firstly i'm glad to be a part of egghead. And honestly pal, you made me understand function scope like it was a piece of cake. Thank you so much. You rock. \m/

Man 1: In the previous videos, we've seen why scope is important in Javascript in general. Now, it's time to take a look at how scope is important in Angular.

I think a lot of people get confused when they first start out because there's also a thing in Angular called scope, but I really want to explain how it's exactly the same thing that we've seen previously as scope in Javascript itself.

To show how this works, we're taking a look at a basic HTML page here. We have two divs. Note that these are siblings of one another. In the first one, we have access to a Javascript scope that's called the first control.

This scope is assigned to a thing called a controller in Angular. Let's not get too tripped up about what this is just now. The important thing to notice about this is that it creates a scope. The second controller creates a second scope.

If we notice these two things are siblings, if we continue our desktop metaphor, they're like having two folders on our desktop, each of which creates a separate thing called posts. We just want to inspect each of those post things.

Let's take a look at our controllers here. We're going to create a thing called posts and assign it to one, two, and three. Down here, we're going to create a thing called posts and assign it to four, five, and six. Let's just take a look at our application so far.

This is a weird thing, right? We expected to see each of these because we have them in our scope. The thing to note about Angular, however, is that it has a special thing called $scope. This is going to take the scope that's created by this function and expose it to our HTML.

This lets our view see the scope that was created by that function. Here, if I say equals post, and I do the exact same thing down here, now we get exactly what we expect. One, two, three, and four, five, and six.

What this allows us to do is to create a nice, public interface that we're able to expose to our HTML. We can hide some of the other intricacies and nuances here inside our controller without exposing it to the view.

Scope resolution of our Angular documents works exactly the same way scope resolution works in plain, old Javascript. The only difference here is that what actually matters about scope inheritance is the structure of our dom.

Here, if I move the second control in here, and I refresh, we get exactly the same thing. The reason is because the second control creates its own scope, and in that scope, we have defined. Let's say is not defined in here.

Now, it becomes equal to one, two, and three. The reason is, if we look back at previous videos, is because the second controller doesn't find post on itself, so lookup proceeds to the parent controller. The only difference, again, being that the dom layout is what dictates how scope resolution works in Angular.

There's a very particular thing in Angular called root scope. Here, what I want to do fact, let's actually just inject this into run. I want to inject the provider root scope. Root scope is the scope that's created at the base of the application.

The base of the application is determined by this NGApp directive. This is where the root scope is defined. Each of the child scopes are new scopes within root scope. This div with the first control is a child of root scope, and this div is a child of the scope created by first control.

If on root scope, what I do is expose post as one, two, and three, well, actually, let's make it four, five, and six so we can see the change here. Now, in fact, let's just delete this guy from here. Both of these have access to the root scope even though neither of their scopes defines this post thing.

Lookup proceeds exactly the same way as it does in traditional Javascript, up through the scope chain. The only difference is the scope chain in Angular is equivalent to our dom hierarchy.

I want to show you why you never want to depend on this. I don't want you worrying about scope inheritance in Angular because it's a really, really bad idea. What we're going to do is just to delete this, and instead, we're going to redeclare equals, in this case, we'll just say one, two, and three.

In this case, we see that our dom is structure such that we have this nesting going on, exactly the same as before. These changed to one, two, and three. Let's say our second controller is relying on this structure to exist.

In any program, we want our components to depend on other components that are likely to change less than they do. There is nothing in the world that changes more often than the layout of a page. If you have designers working on your team, if you have a CEO to report to or a product manager to report to, they're going to want to change the look and the feel of the website on a whim.

If they decided tomorrow that we, in fact, are not going to have this nested structure, suddenly the second controller breaks. We don't want this to happen. Instead of allowing our controllers to break, what we want to do is to isolate our elements so that they're available anywhere.

There are a series of great videos already by John about creating isolate scopes, and that's exactly what John's talking about in those videos. He's showing you how to isolate these scopes so we do not depend on dom hierarchy.

If you're interested in learning a bit more about scoping in Angular, I encourage you to take a look at those videos. This should explain to you how scoping in Angular is exactly like it is in plain, old Javascript.

In the next video, I want to continue to dive into how Angular scoping is similar to Javascript scoping, and we'll also take a look at other directives in Angular that create scopes for us, and how we can use this knowledge of scopes to create robust and isolated Angular elements.

Take a look out for that, guys, and take it easy.

Joel's Head
Why are we asking?