代码区项目交易流程

Angular: control that triggered ng-change


I have an Angular form that has several input boxes that I want to validate, using a generic JS function but need to know the control that fired the event so certain rules can be applied.

(Simplified code)

<input ng-model="Field1" ..... ng-change="validateMe()" /> . . <input ng-model="FieldX" ..... ng-change="validateMe()" /> $scope.validateMe = function() { // get the text of the control that fired the event // do the validation // update something else if valid value given }

I know that ng-click has the $event , but how can I do it from an ng-change

You could have you own custom directive that will call your controller method on custom change event.

Markup <input ng-model="Field1" my-custom-change change-method="validateMe(event)"> <input ng-model="FieldX" my-custom-change change-method="validateMe(event)"> Directive

app.directive('myCustomChangeEvent', function(){ return { scope: { changeMethod = '&' }, link: function(scope, element, attrs){ element.on('change', function(event){ scope.changeMethod({event: event}); scope.$apply(); }); } } })

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

点击收藏

LAST How does Mocha know to wait and timeout only with my asynchronous tests? Dan Abramov - The Melting Pot of JavaScript notes NEXT