Angularjs-ng模型选项,带去盎司和所需,以及一些条件
我有一个表单,其中有一个字段“First Name”Angularjs-ng模型选项,带去盎司和所需,以及一些条件,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我有一个表单,其中有一个字段“First Name” 此字段是必需的,我希望在500毫秒的键入后调用saveEachField(),并且仅当该字段已填充(不是为空时)并且存在设置为true的变量时才调用该字段{{vm.addPage==true} 如果可能的话,在saveEachField()中添加这些条件的另一种方法对我来说将是一条漫长的路 <input type="text" name="first_name" ng-model="vm.adminuser.first_name"
此字段是必需的,我希望在500毫秒的键入后调用saveEachField()
,并且仅当该字段已填充(不是为空时)并且存在设置为true的变量时才调用该字段<代码>{{vm.addPage==true}
如果可能的话,在saveEachField()
中添加这些条件的另一种方法对我来说将是一条漫长的路
<input type="text" name="first_name" ng-model="vm.adminuser.first_name" required aria-invalid="true" ng-change="vm.adminuser.first_name && vm.saveEachField()" ng-model-options="{debounce: 500}
因此,它将减少写入每个ng change中的长ngModel变量&相同的ng change函数
请注意,只有当两个或多个字段有一个ng更改函数时,才使用此解决方案。对于只有一个字段处理,函数中的作用域变量值应该非常简单。或者我们可以使用ng change=“userForm.last\u name.$valid&&vm.saveEachField()”覆盖所有其他验证。
<input type="text" name="first_name" ng-model="vm.adminuser.first_name" required aria-invalid="true" ng-change="vm.adminuser.first_name && vm.saveEachField()" ng-model-options="{debounce: 500}
.directive('checkEachField', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(attrs.ngModel, function (newValue,oldValue) {
//console.log('value changed, new value is: ' + newValue);
if(newValue){
scope.saveEachField();
}
});
}
};
});