doubts with angularjs, input and onblur method ()

1

How do I make an input call a function?

The field is a cep, I want when the user exits this field, the function is called getCep() .

I know that in JavaScript has the function onblur , but with AngularJs I did not find a way to do that.

    
asked by anonymous 16.01.2016 / 23:28

1 answer

5

If your role is within the scope of your controller, you can do it simply by using ng-blur :

 <input type="text" ng-blur="getCep()">

If not, do with onBlur same:

 <input type="text" onblur="getCep()">

Example

function controller($scope){
  $scope.getCep = function(){
  	$scope.demo = $scope.cep;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-appng-controller="controller" >
  
<input type="text" 
       ng-model="cep" 
       ng-blur="getCep()"
       ng-focus="demo = ''"
 ><br>
{{demo}}
  
</div>

In the example above, I just simulated the getCep() function by making it show what was typed when giving a blur in the text box. Likewise I used ng-focus to make var demo empty.

    
16.01.2016 / 23:34