Learn AngularJS: Hour 2

In the first hour of this series we learned how to get started with AngularJS. In this hour we will see Angular Templates and how an AngularJS app can be embraced into a pattern.

Let us start with writing a simple web application with a static template. In the following application we are printing the name of the authors in a HTML unordered list.

  1. <body>  
  2.     <h1>Authors : 8</h1>  
  3.    <ul>  
  4.        <li>Dhananjay Kumar</li>  
  5.        <li>Pinal Dave</li>  
  6.        <li>Glen Block</li>  
  7.        <li>Dan Wahlin</li>  
  8.        <li>Julie Learman</li>  
  9.        <li>Mahesh Chand</li>  
  10.        <li>Prabhjot Singh</li>  
  11.        <li>Gaurav Mantri</li> 

We will get the application rendered as in the following and the authors are shown in HTML using a static template. The following rendered page is pure static HTML page.


Next we will learn to populate Authors dynamically using AngularJS. Before we get into the implementation of this, let us understand that AngularJS apps can be created that adhere to any MV* pattern. For this implementation we will follow the Model-View-Controller pattern. Let us start with creating the controller. The controller can be created in a separate JavaScript file. I have created a JavaScript file, Product.js.

Author.js

  1. var AuthorApp = angular.module('AuthorApp', []);  
  2.   
  3. AuthorApp.controller('AuthorController',function($scope)  
  4. {  
  5.     $scope.authors =  
  6.         [  
  7.                 { 'name''Dhananjay Kumar' },  
  8.                 { 'name''Pinal Dave' },  
  9.                 { 'name''Glen Block' },  
  10.                 { 'name''Dan Wahlin' },  
  11.                 { 'name''Mahesh Chand' },  
  12.                 { 'name''Prabhjot Singh' },  
  13.                 { 'name''Julie Learman' }  
  14.        ];  
  15. }); 

Let us examine the controller class:

  1. In the first line we are creating the Module
  2. In the second line we are creating the Controller.
  3. The controller is a function that takes $scope as parameter.

Then adding an authors JOSN array in the scope of the controller. This is the Model.

In Angular the controller is a constructor function that takes a $scope parameter.

Once we have the controller in place we will need to create the view. The view simply displays the model using templates.

The view and template are created in HTML. We will bind the view with the controller and Angular will project data from the model on the view. On the view we are using various Angular directives.



We created the AuthorApp module and AuthorController controller in Author.js. Now if you see we bind the view and controller in the following three steps:

  1. Implicit Scope Declaration
  2. Scope Inheritance
  3. Model-View Data Binding

In the first step we did a Scope declaration by setting the ng-app directive to the Angular module name.



Then the Scope Inheritance is done by setting the ng-controller directive. After setting the ng-controller directive the scope of the body is set to the controller we created in Author.js.



Now any element inside this body can bound to the array of the controller.



This is the simple procedure you need to follow to work with Angular templates and controllers. For your reference the code of the view and template is given below,

  1. <!DOCTYPE html>  
  2. <html ng-app="AuthorApp">  
  3. <head>  
  4.     <title>Angular Demo</title>  
  5.     <script src="Scripts/angular.min.js"></script>  
  6.     <script src="Author.js"></script>  
  7. </head>  
  8. <body ng-controller="AuthorController">  
  9.     <ul>  
  10.         <li ng-repeat="author in authors">  
  11.             {{author.name}}  
  12.         </li>  
  13.     </ul>  
  14. </body>  
  15. </html> 

In the next hours we will get into other aspects of AngularJS.

Up Next
    Ebook Download
    View all
    Learn
    View all