angular FormController验证

时间:2015-04-28 10:08:06

标签: angularjs

我有这个HTML格式:

<div class="modal-body">
        <form name="addAdminForm">
            <div class="form-group addPopupLabel">
                <div class="container-fluid-full" id="email3">
                    <input placeholder="Email" type="text" ng-model="model.email" required />
                </div>

                <div class="container-fluid-full">
                    <input placeholder="Password (at last 6 characters)" type="password" ng-model="model.password1" id="pw1" name="pw1" required />
                </div>

                <div class="container-fluid-full">
                    <input placeholder="Confirm password" type="password" ng-model="model.password2" id="pw2" name="pw2" required password-compare="pw1" />
                </div>
                <div class="container-fluid-full">
                    <label>Admin</label>
                    <input type="radio" class="form-control" name="role" ng-model="model.role" ng-value="userRoles.admin">
                </div>

                <div class="container-fluid-full">
                    <label>CS</label>
                    <input type="radio" class="form-control" name="role" ng-model="model.role" ng-value="userRoles.cs">
                </div>

                <div>
                    <span class="errormessage" style="color:red">{{errormessage}}</span>
                </div>
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" ng-disabled="addAdminForm.$invalid" ng-click="createForm.$invalid || ok()">Submit</button>
        <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
    </div>

问题:当其中一个字段处于焦点时,按钮保持在禁用模式。 我如何通过FormController解决它?

1 个答案:

答案 0 :(得分:1)

addAdminForm的范围在表单中。因此,一种选择是移动表单中的按钮,或移动表单元素以便它包裹按钮。我更喜欢这个,因为它很简单,而且大部分时间都可行。

如果由于某种原因不可能,您可以制定一个指令,将表单的$invalid标志导出到外部作用域的变量中。一个简单的实现是:

app.directive('bindValidity', ['$parse', function($parse) {
    return {
        restrict: 'A',
        scope: false,
        controller: ['$scope', '$attrs', function($scope, $attrs) {
            var assign = $parse($attrs.bindValidity).assign;

            if (!angular.isFunction(assign)) {
                throw new Error('the expression of bindValidity is not settable: ' + $attrs.bindValidity);
            }

            this.setFormController = function(formCtrl) {
                if (!formCtrl) {
                    throw new Error('bindValidity requires one of <form> or ng-form');
                }
                $scope.$watch(
                    function () {
                        return formCtrl.$invalid;
                    },
                    function (newval) {
                        assign($scope, newval);
                    }
                );
            };
        }],
        require: ['?form', '?ngForm', 'bindValidity'],
        link: function (scope, elem, attrs, ctrls) {
            var formCtrl, bindValidity;
            formCtrl = ctrls[0] || ctrls[1];
            bindValidity = ctrls[2];
            bindValidity.setFormController(formCtrl);
        }
    };
}]);

将其用作:

<div class="modal-body">
    <form name="addAdminForm" bind-validity="some.variable">
        ...
    </form>
    <div class="modal-footer">
        <button ... ng-disabled="some.variable" ng-click="some.variable || ok()">Submit</button>