From the course: Building a Single-Page Application with AngularJS and WordPress

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Creating an AngularJS Controller

Creating an AngularJS Controller

From the course: Building a Single-Page Application with AngularJS and WordPress

Start my 1-month free trial

Creating an AngularJS Controller

- Angular controllers are easy to create. The main purpose for a controller is so Angular knows what functionality to run based on the route which we will create later. We'll create a controller, and call it ListCtrl. ListCtrl will be the controller that runs on a blog listing or archive page. As I said before, controllers can take on injectable objects, like scope and the Posts factory, which we just created. Now we have a function that will run when the route matches the controller. I'm going to go ahead and put something in the console, so we know that it's running fine. I'm going to go ahead and also define out your variable called page_title, and just call it a string of Blog Listing. And finally, I'm going to use our Posts factory and query the API route that we have defined it to, and make the response equal to scope.posts. Now that we have the controller in place, all we need is some HTML and a route to view the work.

Contents