Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs-ng模型选项,带去盎司和所需,以及一些条件_Angularjs_Angularjs Directive - Fatal编程技术网

Angularjs-ng模型选项,带去盎司和所需,以及一些条件

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"

我有一个表单,其中有一个字段“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();
                }
            });
        }
    };
});