Learn AngularJS: Hour 3

In hour 1 of this series we learned how to get started with AngularJS.

In hour 2 of this series we learned about AngularJS Templates.

In this hour we will learn about:

  1. Filter
  2. orderBy
  3. Two-way binding

To start with I have put a search box with a ng-model directive set.



Now the filter can be applied to the repeater directive by providing an option filter. In the filter we need to set a value as:



Let us consider understanding what exactly is happening here. The first thing is Data Binding that is the core feature of AngularJS. Angular binds the name of the input box to a variable in the data model. This is two-way binding. Two-way binding keeps an input box and data model in sync.



Later we are using the filter function. This function will use an input box ng-model value to create a new array that will contain a filtered value based on the matched ng-model value from the input box. Next let us see how easy the orderBy function can be implemented in AngularJS. Let us say we have a drop down as below:



We have created a drop down with two options. Both options have a value as the properties of the controller. Next just to sort the order set orderBy function value to the ng-model value of the drop down list.



For your reference the full source code for the Controller is given below.

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

The source code of the View 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.   
  10.     Search: <input ng-model="searchtext">  
  11.   
  12.     Sort:  
  13.     <select ng-model="sortorder">  
  14.         <option value="name">Name</option>  
  15.         <option value="age">Age</option>  
  16.     </select>  
  17.   
  18.     <ul>  
  19.         <li ng-repeat="author in authors |  
  20.             filter:searchtext |  
  21.             orderBy:sortorder">  
  22.             {{author.name}} is {{author.age}} years old  
  23.   
  24.        </li>  
  25.     </ul>  
  26. </body>  
  27. </html> 

I hope you find this hour 3 of AngularJS series helpful. Thanks for reading. Happy coding.

Up Next
    Ebook Download
    View all
    Learn
    View all