doubts with Angularjs, input and onblur() method

Asked

Viewed 3,090 times

1

How do an input call a function?

The field is a zip code, I want when the user leaves this field to be called the function getCep().

I know that in the JavaScript has the function onblur, but with AngularJs I couldn’t find a way to do it.

  • For an input to call a function it is not necessary to call the Angular, unless you want to change the state of an object at the angular, <input onblur="SuaFuncao();" />

1 answer

5

If your function is within the scope of your controller, you can simply do so using the ng-blur:

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

If not, do so with the onBlur even:

 <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>

<div ng-app ng-controller="controller" >
  
<input type="text" 
       ng-model="cep" 
       ng-blur="getCep()"
       ng-focus="demo = ''"
 ><br>
{{demo}}
  
</div>

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.