⚠️ This lesson is retired and might contain outdated information.

Dynamically Display Data with AngularJS Controllers

Lukas Ruebbelke
InstructorLukas Ruebbelke
Share this video with your friends

Social Share Links

Send Tweet

We are going to take the static HTML elements and dynamically display them on the page by setting up an AngularJS controller and hooking it up with AngularJS templating in the view.

The code for this lesson can be found on Github. Note: the tag name corresponds to the lesson.

Man: [00:00] Welcome to the next video in the Eggly series where I am going to show you how to create an AngularJS module, as well as hook up an AngularJS controller and then make that available to the view to actually render data structures that you define in your controller.

[00:20] What we're going to do in this application is actually take these category elements and these bookmark elements and define them in the controller. Then using ngRepeat and AngularJS templating then render them back out in the view.

[00:34] The first thing that we need to do is actually define the module that we are going to organize our code into. An AngularJS module is simply an organizational mechanism to group functionality. It's a way for, for instance, if you have users then you might group the user functionality into a user submodule. If you have, for instance, a bookmarks functionality or a set of bookmarks functionality you can organize that into a bookmarks submodule.

[01:01] You'll see this actually in later videos when we start to get into more advanced topics. For now, I'm going to go to the ngApp directive and I'm just going to add a value to this attribute and I'm going to say, "Eggly." When this instantiates it's going to look for a module called Eggly."

[01:19] We need to define this module, but the first thing that we need to do is actually reference our source file for our application. It's Eggly app.start. Let's hop over to here. You define your Angular module using Angular.module. This takes two arguments, the name of the module and then an array of dependencies. In this case, we do not have any dependencies yet but you could put any submodule in there. For instance, if you're doing routing or animating then you would put ngRouter ngAnimate.

[02:02] From there, we are going to define our controller. Let's call this "Main Controller." This takes a second parameter, either an array with submodules or dependencies, and then the last element of that array being the actual functionality for the controller in the form of a function or it can just take a function. I prefer to use the function and then actually define my dependencies using ngMin or in a post-production step. In this case, we're just going to pass in scope like so.

[02:46] Just to show that this is working let's create a variable called "Hello." This is going to be similar to what we did before. We're going to instantiate this to World. Let's hop back in here. We have to actually define the controller in our HTML. We're going to use ngController. We're going to set this to main controller. Now this controller controls the element that it was defined on and all of its subelements or children elements.

[03:25] From here I'm just going to go, "H1 {hello}." Let's refresh the page. This should evaluate to "World." Now our controller is hooked up. It's live. Let's delete this. What I'm going to do now is I am going to define a categories and bookmarks array in this controller. I'm not actually going to type this out. I'm just going to paste this in here.

[03:55] What I have is scope.categories. It's an array with objects for categories with an ID and a name. Then, bookmarks is also an array of objects with ID, title, URL, and category. You can see that the category actually matches up to the category that it's in.

[04:14] Now that I've defined categories and bookmarks on scope, it's now available in the view for binding. Let's go ahead and do that. Now you can see that I have these three category elements. Let's delete this one.

[04:36] You can see that we have three category elements. Let's delete this one. We want to actually repeat this list item element. We're going to go here. Using ngRepeat, we're going to say, "ngRepeat category in categories." What this does is it looks for the categories array and then it will basically stamp out this element once per element or item in the collection.

[05:16] From here, we simply can do the double curly braces and category. That's a variable that keeps track of the current category that it's referencing in the categories array. Let's put category.name. Let's hop into the HTML, refresh. Now you can see that we have these four categories here. Development, design, exercise, and humor. Let's do the same thing for the bookmarks.

[05:47] We don't need these bottom two but we do want to repeat the first bookmark. Let's go "ngRepeat bookmark in bookmarks" like so. Then, from here, let's change this Href to the "bookmark.url," and let's change this to "bookmark.title." When we refresh this now, we should have categories that are dynamic and we have the bookmarks that are also rendering dynamically as they are being pulled from the controller.

[06:39] Just to review what we've done, we've taken ngApp and now we are pointing it to the Eggly submodule which we defined up here, Angular.module Eggly, and then on top of that module we have defined a main controller with two arrays of objects, category and bookmarks.

[06:57] Because we have put that on scope that is available for the view. Think of scope as the glue between the controller and the view. Then using the ngRepeat syntax we are repeating over categories and bookmarks and, in this case, category and categories as we can now reference that object and bind to it. Now we're binding into displaying the category name. Then down here we are binding to the URL and the bookmark title.

[07:32] In the next video, I'm going to show you how, when you actually click on a category, we can actually filter out and show just the bookmarks for that category. We've seen how to actually bind two properties in the controller but now we're going to take it one step further and we're going to actually show how to add in functionality or communicate back to the controller from the view.

[07:54] Thanks for sticking around for this video. I look forward to seeing you in the next one.