Introduction to AngularJS

AngularJS - Basics

AngularJS is a JavaScript framework based on the MVC design pattern. It is usually used for client-side web application development. It is best suited for the implementation of the Single Page Application Architecture (SPA). It is an OpenSource technology developed and supported by Google.

As in AngularJS Docs “AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. Angular's data binding and dependency injection eliminate much of the code you would otherwise need to write. And it all happens within the browser, making it an ideal partner with any server technology.”

MVC is popular because it isolates the application logic from the user interface layer and supports separation of concerns. A Model View Controller pattern is made of the following three parts:

  • Model: maintains data.
  • View: represents data.
  • Controller: controls the communications between the Model and View.

First Application: Hello world

  1. <html>  
  3. <title>AngularJS : Hello World</title>  
  4. <body>  
  5. <h1>AngularJS : Hello World</h1>  
  6. <div ng-app="">  
  7.    <p>Enter Name: <input type="text" ng-model="name"></p>  
  8.    <p>Hello {{name}}!</p>  
  9. </div>  
  11. </body>  
  12. </html>  
Here we have added the Angular library with the <Script> tag as in the following:
Now note the line <div ng-app=""> where “ng-app” indicates that we have an angular application in the <div>. It is also used to load various AngularJS modules into the AngularJS Application. “ng-model” helps to bind the values of AngularJS application data to HTML input controls. The {{ }} expression is an AngularJS data binding expression. Here we can also use “ng-bind” to bind the model value to our HTML page.

With ng-bind “<p>Hello {{name}}!</p>” is changed to <p>Hello <span ng-bind=name></span>!</p>

We can also initialize the model data with “ng-init”.

  1. <div ng-app="" ng-init="name='ratnesh'">  
  4. </div>  

We can also initialize two or more values in our model as in the following:

  1. <div ng-app="" ng-init="name='ratnesh';country='india'">  
  3.    <p>Hello <span ng-bind=name></span>! Is it cold in <span ng-bind=country></span> ?</p>  
  4. </div>  

“ng-repeat” is also a very common directive. It is used to repeat an HTML element. In the following lines of code we are repeating the “<tr>” for each item in the model called “friends”.
  1. <div ng-app="" ng-init="friends=[  
  2. {name:'Rahul',city:'Banglore'},  
  3. {name:'Rajib',city:'Pune'},  
  4. {name:'Biswadeep',city:'Dhanbad'}]">  
  6. <table border="1">  
  7.   <tr ng-repeat="x in friends">  
  8.     <td>{{}}</td>  
  9.     <td>{{}}</td>  
  10.   </tr>  
  11. </table>  
  12. </div>  

AngularJS: Controllers

In an AngularJs application, the Controller is a JavaScript object to control the flow of data in the application. It is defined by the ng-controller directive. It accepts $scope as a parameter that refers to the current application.

Now we are trying to build an application that takes the input form user as name and score and calculates the percentage for the score out of 950 and displays the message. Here we have defined a controller as “CalulateController” that takes the argument $scope. The ng-model directives bind the input fields to the controller properties (name and score). The property percent is the function of the current $scope object that returns the percentage.

  1. <div ng-app="" ng-controller="CalulateController">  
  2. Name: <input type="text" ng-model="Name"><br>  
  3. Score:<input type="text" ng-model="score"><br>  
  4. <br>  
  5. <span ng-if="Name && score">  
  6. {{Name}} have scored {{score}} i.e {{percent()}} %  
  7. </span>  
  8. </div>  
  10. <script>  
  11. function CalulateController($scope)   
  12. {     
  13.     $scope.percentfunction() {return $scope.score / 950*100;}        
  14. }  
  15. </script>  
  16. </body>  
Please note the ng-if directive that helps us to hide and unhide the message.

Now we can also move our scripts into a separate file, say scripts.js and link it to our HTML file.

AngularJS: Filters

Filters are used to transform the data when displaying. They can be used in expressions and directives using the pipe (|) character. The following are the commonly used filters:

S.No Name Description
1 uppercase Used to convert text to upper case text
2 lowercase Used to convert text to lower case text
3 currency Used to format text in a currency format
4 filter Used to filter the array
5 orderBy Used to sort the array

Now see the the following code shipment as an example of filters.

  1. <html>  
  3. <title>AngularJS : Filters</title>  
  4. <body>  
  5. <h3>AngularJS : Filters</h3>  
  8. {name:'Rahul',city:'Banglore',salary:1523},  
  9. {name:'Rajib',city:'Pune',salary:3500},  
  10. {name:'Biswadeep',city:'Dhanbad',salary:2583}]">  
  12. <p>Search : <input type="text" ng-model="FilterText"></p>  
  14. <table border="1">  
  15.     <tr>  
  16.         <th>Name</th>  
  17.         <th>City</th>  
  18.         <th>Salary</th>  
  19.     </tr>  
  20.     <tr ng-repeat="x in friends| orderBy:'city'| filter:FilterText">  
  21.         <td>{{|uppercase}}</td>  
  22.         <td>{{|lowercase}}</td>  
  23.         <td>{{x.salary|currency}}</td>  
  24.     </tr>  
  25. </table>  
  26. </div>  
  27. </body>  
  28. </html>  

Here we have initialized the friends array and each item has a name, city and salary. We have created the table earlier but this time we have transformed the name in uppercase, city in lowercase and salary as currency. Note that in the “ng-repeat” we have added the two filters “orderBy” and “filter”. Order by sorts the data by city. By default it is ascending. If we need to sort the table descending then we need to change it to orderBy:'-city'.

We have use “filter” to filter the table data depending on the value in the text box or model value, in our case the model is “FilterText”. It will by default search the text in the entire row. We can also set it to a specific column as well. Suppose we need to search within the name only. The ng-model needs to be changed to ng-model="".