User123 User123 - 3 months ago 13
AngularJS Question

How to call function after leaving the particular input

I have 2 inputs, I wanted to call a function after switching to second input. I tried it by

ng-change
but that will call function on each value.

Can any one suggest me how to call a function after switching to next input.

Fiddle

<div ng-controller="TodoCtrl">

<input type="text" ng-model="todoText1" size="30" placeholder="add new todo here"><br>

<input type="text" ng-model="todoText2" size="30" placeholder="add new todo here"><br>


<input class="btn-primary" type="submit" value="add">



Answer

You can call function on ng-blur if you want to call function as soon as you leave the field or you can call function on ng-focus of next input if you want to call function as soon as you switch to next field

<input type="text" ng-model="todoText1"  size="30"
     placeholder="add new todo here" ng-blur ="myfunction()">

<input type="text" ng-model="todoText2"  size="30"
     placeholder="add new todo here" ng-focus ="myfunction()">

From this two i will choose ng-blur because many time i found performance issue on ng-focus

Comments