ngChange And ngBlur Directives In AngularJS

Introduction

AngularJS provides many built-in directives. In this article we will discuss about ngChange and ngBlur directives.

ngChange

This directive allows us to define custom behavior on element change event. This directive has highest priority. Here changes are applied immediately while in the JavaScript “onchange” event which only triggers at the end of all change. The ngChange directive only access the value when changes made in input value and new value committed to the model. This directive required ngModel directive.

Syntax

< <input ng-change="expression" ng-model=”expression”>
...
</input >


Example

HTML

  1. <h4>ngChange Example</h4>  
  2. <div ng-controller="HomeController">  
  3.     <input type="text" ng-change="change()" ng-model="textChange1" placeholder="Change event example">  
  4.     <br />  
  5.     <label>Change: {{textChange}} </label>  
  6. </div>  
Controller
  1. var app = angular.module("app", []);  
  2. app.controller("HomeController", function ($scope)  
  3. {  
  4.     $scope.textChange = "No"  
  5.     $scope.change = function ()  
  6.     {  
  7.         $scope.textChange = "Yes"  
  8.     }  
  9. });  
Output

ngChange

ngBlur

This directive allows us to define custom behavior on element blur event. This directive has highest priority.

Syntax

< <input, select, window, textarea, a ng-blur="expression">
...
</input, select, window, textarea, a >


Example

HTML

  1. <h4>ngBlur Example</h4>  
  2. <div ng-controller="HomeController">  
  3.     <input ng-blur="blur()" placeholder="Blur event example">   
  4. </div>  
Controller
  1. var app = angular.module("app", []);  
  2. app.controller("HomeController", function ($scope)  
  3. {  
  4.     $scope.blur = function ()  
  5.     {  
  6.         alert('Perform blur event');  
  7.     }  
  8. });  
Output

ngBlur

Summary

The ngChange and ngBlur are important directives to perform change and blur event in AngularJS. This article will help you to learn all of them. 

Up Next
    Ebook Download
    View all
    Learn
    View all