在angularjs中,如果输入有错误,如何在表单提交上设置焦点?

时间:2015-05-22 05:31:16

标签: angularjs

这是我的代码,如果第一个名称文本框包含$error.required$error.pattern$error.minlength$error.maxlength错误,我希望将重点放在表单提交上的名字文本框中

<form class="form-horizontal" name="clientForm" id="clientForm" novalidate ng-submit="clientForm.$valid" ng-class="{ loading:form.submitting }">
<div class="form-group">
    <label class="col-lg-2 control-label">First Name</label>
    <div class="col-lg-8">
        <input type="text" ng-model="client.firstName" class="form-control" required autofocus name="firstName" autocomplete="off" ng-maxlength="100" ng-minlength=3 ng-pattern="/^[a-zA-Z]*$/" />
        <!--<span ng-show="clientForm.firstName.$dirty && clientForm.firstName.$invalid" class="error">First Name is required.</span>-->
        <div class="errors" ng-show="clientForm.$submitted || clientForm.firstName.$touched">
            <div class="error" ng-show="clientForm.firstName.$error.required">
                First Name is required.
            </div>
            <div class="error" ng-show="clientForm.firstName.$error.pattern">
                Enter valid name.
            </div>
            <div class="error" ng-show="clientForm.firstName.$error.minlength">
                First Name is required to be at least 3 characters
            </div>
            <div class="error" ng-show="clientForm.firstName.$error.maxlength">
                First Name cannot be longer than 100 characters
            </div>
        </div>
    </div>
</div>
<button type="submit" class="btn btn-primary">Save</button></form>

3 个答案:

答案 0 :(得分:6)

这个问题与以下内容重复: Set focus on first invalid input in AngularJs form

您可以在表单上使用指令:

<form accessible-form>
    ...    
</form>

app.directive('accessibleForm', function () {
    return {
        restrict: 'A',
        link: function (scope, elem) {

            // set up event handler on the form element
            elem.on('submit', function () {

                // find the first invalid element
                var firstInvalid = elem[0].querySelector('.ng-invalid');

                // if we find one, set focus
                if (firstInvalid) {
                    firstInvalid.focus();
                }
            });
        }
    };
});

这是一个有效的Plunker: http://plnkr.co/edit/wBFY9ZZRzLuDUi2SyErC?p=preview

答案 1 :(得分:0)

您可以尝试这样做:即在名字输入中添加ng-focus="clientForm.$error"

<input type="text" ng-focus="clientForm.$invalid" ng-model="client.firstName" class="form-control" required autofocus name="firstName" autocomplete="off" ng-maxlength="100" ng-minlength=3 ng-pattern="/^[a-zA-Z]*$/" />

答案 2 :(得分:0)

您必须使用指令处理此问题。例如:

它将迭代元素并检查focusNow属性是否为真。确保错误处理程序代码将表达式设置为true / false。

.directive('focusNow', function ($timeout) {
    return {
        link: function (scope, element, attrs) {
            scope.$watch(attrs.focusNow, function (value) {
                if (value === true) {
                    for (var i = 0; i < element.length; i++) {
                        var ele = angular.element(element[i].parentNode);
                        if (!ele.hasClass('ng-hide')) { //Skip those elements which are hidden.
                            element[i].focus();
                        }
                    }
                }
            });
        }
    };
});

并在HTML中,您将拥有:

<input type="text" focus-now="expression" />

其中expression将是一个正常的表达式,如果出错,它将被评估为真。