Hello. In this lesson, we are going to press pause for just a moment, address the issue of scope inheritance, and then do a quick refactor to fix it before we get into our models. Let's see what the problem is. Let's hop into our bookmarks.js file.
You can see that we are only binding to one property on scope. That is currentCategoryName. With that in mind, let's hop into the bookmarks template. You can see that we are binding to this property, currentCategory, right here.
The question is "What about all these other things that we're binding to?" For instance, this bookmarks collection in ng-repeat or these methods that we're binding to in ng-click. Where do those exist? Obviously, they're not in the bookmarks controller.
The answer to that question is they are in this main controller here. You can see the bookmarks collection, these different methods that we're making available onto the main controller and then making it available on the body tag. It's the top-most controller.
In AngularJS, scope objects prototypically inherit from their parent scope object all the way up to root scope. This creates an interesting situation where the main controller is the parent controller to the bookmarks controller.
We are implicitly attaching the scope object or the properties or methods from main controller onto the scope object of the bookmarks controller. This may seem convenient at first, but when you have an application that's growing in size and complexity, this implicit inheritance is just a recipe for unintended consequences.
For instance, what would happen if I changed a property on this bookmarks collection and this URL property actually got changed to local URL or some other property?
This template would break, and you wouldn't be able to understand why by looking at the bookmarks controller because you have this implicit scope inheritance. You're actually pulling from a property that is much further up the prototype chain.
The first thing we're going to do is jump into our state definition and just go "BookmarksCtrl as bookmarks." In our bookmarks controller, we are going to delete the scope object.
Now, we have assigned currentCategory to bookmarks. Let's go back to our template. It's now bookmarks.currentCategoryName. Now, this reads a little bit better, and it's a little bit more precise of where currentCategoryName is coming from.
It's on the bookmarks object that we defined in our state definition. It's "BookmarksCtrl as bookmarks," and then we're referencing this property, currentCategoryName, on bookmarks. Let's refresh the page. Let's go to Development. You can see that this is still working.
The interesting thing is the implicit inheritance chain is broken. Now, we're no longer reaching out of our controller into a parent controller and pulling down the bookmarks collection, which is why in the next lesson we are going to actually explicitly provide that to our bookmarks controller in the bookmarks model and make that available. Stay tuned for the next lesson. I'll see you then.