我有一个类型为email
的输入:
<input type="email" ng-model="user.email" name="email" required>
现在我需要每当输入改变时执行一些代码。添加 ng-change="emailInputChanged()"
仅执行我的方法,以防输入的字符串是有效的电子邮件地址,但我需要在每次击键时执行回调,即使输入未验证。 (在观看模型时当然会出现同样的问题 à la $scope.$watch('user.email', emailInputChanged)
。
有什么 Angular 方法吗?
最佳答案
我建议使用 onInput 事件:http://www.w3schools.com/jsref/event_oninput.asp 创建自定义指令并监听文本字段元素上的“input”事件。
指令实现示例:
function OnInput() {
return {
restrict: 'A',
template: '',
scope: {
whenInputChanged: '&'
},
link: function($scope, $element, $attrs) {
$element.on('input', $scope.whenInputChanged);
}
};
}
angular.module('App')
.directive('onInput', OnInput);
在模板中应用事件监听器:
<input type="email" on-input when-input-changed="change()" name="email" required/>
尽情享受吧!
关于javascript - 每当输入更改时 AngularJS ng-change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32692135/