How to use Angular onBlur?

Asked

Viewed 6,234 times

1

I want it to be shown on console the value cep I typed in the field input after the field loses focus. But this code of mine is not doing that.

html:

<body ng-controller="appController">
<div align="center">
    <form>
        <label>CEP </label><br>
        <input type="text" ng-model="endereco.cep" ng-blur="pegaCep(endereco)"><br>
        <label>Estado </label><br>
        <input type="text" ng-model="endereco.estado"><br>
    </form>
</div>
</body>

Angular:

app.controller('appController', function ($scope, $http){
    var pegaCep = function (data) {
        console.log(data);
    }
    pegaCep()
});
  • 6

    Gustavo, I imagine your question already has an answer here: http://answall.com/questions/108712/duvida-com-angularjs-input-e-metodo-onblur/108713#108713.

  • The code of this other post, for me, did not work... Kept appearing on the console that my pegaCep had not been defined.

1 answer

1


var app = angular.module('myApp', []);

app.controller('appController', function ($scope, $http){
  $scope.pegaCep = function () {
    console.log($scope.endereco.cep);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app='myApp' ng-controller="appController">
<div   align="center">
    <form>
        <label>CEP </label><br>
        <input type="text" ng-model="endereco.cep" ng-blur="pegaCep()"><br>
        <label>Estado </label><br>
        <input type="text" ng-model="endereco.estado"><br>
    </form>
</div>
</body>

Browser other questions tagged

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