javascript - AngularJS ng-change whenever input changes -


i have input of type email:

<input type="email" ng-model="user.email" name="email" required> 

now need execute code whenever input changed. adding ng-change="emailinputchanged()" executes method, in case entered string valid e-mail address, need callback executed every keystroke, though input not validate. (same issue of course when watching model à la $scope.$watch('user.email', emailinputchanged).

any angular way so?

i suggest use oninput event: http://www.w3schools.com/jsref/event_oninput.asp create custom directive , listen "input" event on text field element.

example of directive implementation:

function oninput() {   return {     restrict: 'a',     template: '',     scope: {       wheninputchanged: '&'     },     link: function($scope, $element, $attrs) {       $element.on('input', $scope.wheninputchanged);     }   }; }  angular.module('app')   .directive('oninput', oninput); 

to apply event listener in template:

<input type="email" on-input when-input-changed="change()" name="email" required/> 

enjoy!


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -