首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将所有输入设置为$touched而不提交按钮AngularJS

如何将所有输入设置为$touched而不提交按钮AngularJS
EN

Stack Overflow用户
提问于 2015-12-08 19:14:42
回答 2查看 11.9K关注 0票数 2

我有一个指令,根据表单的$valid$untouched属性验证表单中的输入--如果输入被“触摸”,它会检查验证,并相应地将字体和边框用红色/绿色表示,如果输入没有“被”“触摸”,则不会做任何事情。

我使用ngBootBox的自定义对话框,所以我没有type="submit"类型的按钮来提交表单,而是使用"Create“按钮的回调函数来传递/保存数据。

我的问题是,当我单击"create“按钮而表单不”有效“时,因为其中一些字段是空的--我的输入仍然是”未触及“的,因此不会调用$watch函数。有什么解决办法吗?有什么方法可以这样做:$scope.createProjectForm.$setTouched(true);将使该形式的所有子输入获得该值吗?

我也试过了,但没用:

代码语言:javascript
复制
angular.forEach($scope.createProjectForm, function(field){
   field.$setTouched(true);
});

这是我的验证指令:

代码语言:javascript
复制
angular.module('mean.theme').directive("inputValidation", function () {
    return {
        restrict: 'EA',
        require: 'ngModel',
        link: function (scope, inputElement, attrs, ngModelCtrl) {
            var $icon = $('<i class="fa"></i>');
            inputElement.before($icon);
            scope.$watchGroup([
                function(){
                    return ngModelCtrl.$untouched;
                },
                function(){
                    return ngModelCtrl.$valid;
                }
            ], function(Paramaters){
                console.log(scope);

                if(!Paramaters[0]) {
                    if (Paramaters[1]) {
                        inputElement.switchClass('validation-warning-border','validation-success-border',50)
                        inputElement.prev().switchClass('fa-warning validation-warning-font' , 'fa-check validation-success-font',50);
                    } else {
                        inputElement.switchClass('validation-success-border','validation-warning-border',50)
                        inputElement.prev().switchClass('fa-check validation-success-font','fa-warning validation-warning-font',50)
                    }
                }
            });
        }
    };
});

这是我的控制器代码的一部分:

代码语言:javascript
复制
    $scope.create = function () {
        var options = {
            title: 'Create',
            templateUrl: 'project.html',
            scope: $scope,
            buttons: {
                warning: {
                    label: "Cancel",
                    className: "btn-link",
                    callback: function () {
                    }
                },
                success: {
                    label: "Create",
                    className: "green",
                    callback: function () {
                        if ($scope.createProjectForm.$valid){
                            $scope.createProject(template);
                        } else {
                            $scope.project.createButtonClicked = true;
                            return false;
                        }
                    }
                }
            }
        };
        $ngBootbox.customDialog(options);
    };

这是我HTML代码的一部分:

代码语言:javascript
复制
<form role="form" name="createProjectForm">
    <label>
        Name Your Project
    </label>
    <div>
        <input type="text" name="project.name"
               ng-model="project.name" required="required" class="form control" input-validation/>
    </div>
    <label>
        Name Your Project
    </label>
    <div>
        <input type="text" name="project.title"
               ng-model="project.title" required="required" class="form control" input-validation/>
    </div>
</form>
  • 编辑:

我找到了我所需要的,更简单、更短的方法:

可以手动设置:$scope.createProjectForm.$setSubmitted()true

然后,为该更改设置子(输入) $watch

代码语言:javascript
复制
scope.$watchGroup([
            function(){
                return ngModelCtrl.$untouched;
            },
            function(){
                return ngModelCtrl.$valid;
            },
            function(){
                return ngModelCtrl.$$parentForm.$submitted;
            }
        ], function(Paramaters){
         // code here
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-08 20:04:26

您可以采用类似于此的格式:

代码语言:javascript
复制
if ($scope.form.$invalid) {
    angular.forEach($scope.form.$error, function (field) {
        angular.forEach(field, function(errorField){
            errorField.$setTouched();
        });
    });
}

见'$setTouched':https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

票数 11
EN

Stack Overflow用户

发布于 2017-02-08 12:29:02

对于嵌套表单:

代码语言:javascript
复制
function setFormTouched(form) {
        // Check if the form/property has the $setSubmitted method
        if (form.hasOwnProperty('$submitted')) {
            // Iterate through each of the required error properties
            angular.forEach(form.$error, function (errorType) {
                // Iterate through each error type
                angular.forEach(errorType, function (prop) {
                    // Check if the property has the $setTouched method
                    if (prop.hasOwnProperty('$touched')) prop.$setTouched();
                    // Recursive call to handle nested forms
                    setFormTouched(prop);
                });

            });
        }
    }

解决方案基于Patrick的码页,但是我不得不编辑原始代码,因为hasOwnProperty不适用于原型中定义的函数。例如:

代码语言:javascript
复制
if(prop.hasOwnProperty('$setTouched'))

都是假的。

编辑:我最初需要的是一种使用户更容易在嵌套选项卡中查找错误的方法。这是用于禁用/启用submit按钮和错误消息的最后工作范围函数:

代码语言:javascript
复制
$scope.isFormValid = function (topLevelForm) {
        if (topLevelForm.$valid) {
            return true;
        }

        // Form not valid, triggering fields to make it easier to find errors
        setFormTouched(topLevelForm);

        function setFormTouched(form) {
            // Check if the form/property has the $setSubmitted method
            if (form.hasOwnProperty('$submitted')) {
                // Iterate through each of the required error properties
                angular.forEach(form.$error, function (errorType) {
                    // Iterate through each error type
                    angular.forEach(errorType, function (prop) {
                        // Check if the property has the $setTouched method
                        if (prop.hasOwnProperty('$touched')) prop.$setTouched();
                        // Recursive call to handle nested forms
                        setFormTouched(prop);
                    });

                });
            }
        }
        return false;
    };
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34164122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档