繁体   English   中英

表单验证与隔离伪指令相结合将删除范围值

[英]Form validation combined with isolated directive is deleting scope value

当我将带有隔离范围的指令与自定义验证结合使用时,当验证失败时,验证会将我的scope值设置为undefined

这是带有代码的jsfiddle: http : //jsfiddle.net/5mKU3/7/

的HTML:

<div ng-app="myApp">
    <div ng-controller="example">
        someModel: {{someValue}}<br>
        <input type="text" isolate ng-model="someValue" validate />
    </div>
</div>

JS:

angular.module('myApp', []).directive('isolate', function () {
    return {
        require: 'ngModel',
        scope: {}
    };
}).directive('validate', function () {
    return {
        require: 'ngModel',
        link: function (scope, element, attrs, ctrl) {
            ctrl.$parsers.unshift(function (value) {
                value = value || '';

                if (value.length < 10) {
                    ctrl.$setValidity('fail', true);
                    return value;
                } else {
                    ctrl.$setValidity('fail', false);
                    return undefined;
                }
            });
        }
    };
}).controller('example', function ($scope) {
    $scope.someValue = '1234';
});

输入超过10个字符时,验证将失败。 如何验证失败时, $scope.someValue不会重置为undefined

我当前使用的angular版本是1.2.18。

ngModelOptions.allowInvalid设置为true

您的输入如下:

<input type="text" isolate ng-model="someValue" validate ng-model-options="{allowInvalid:true}"/>

$validate()方法的描述中验证此信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM