gpt4 book ai didi

javascript - Angular : Control that fired ng-change

转载 作者:行者123 更新时间:2023-11-28 00:00:58 26 4
gpt4 key购买 nike

我有一个 Angular 表单,其中有几个我想要验证的输入框,使用通用 JS 函数,但需要知道触发事件的控件,以便应用某些规则。

(简化代码)

<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

}

我知道 ng-click$event,但是我如何从 ng-change 中做到这一点

最佳答案

您可以拥有自己的自定义指令,该指令将在自定义更改事件上调用 Controller 方法。

标记

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

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

指令

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

关于javascript - Angular : Control that fired ng-change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31809268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com