【问题标题】:Password checkbox field AngularJS min/maxlength issue密码复选框字段AngularJS最小/最大长度问题
【发布时间】:2013-04-23 10:32:10
【问题描述】:

我创建了一个密码字段,在选中复选框时显示密码。
我正在使用 ng-minlenght 和 ng-maxlength 来控制密码长度。

当密码介于输入字段的最小和最大长度之间时,它会按应有的方式显示密码文本。

但是当密码无效/不在字段的最小和最大长度之间时,我得到一个空值。

Plnkr Example

这是 Angular 中的错误还是我做错了什么?

【问题讨论】:

    标签: forms angularjs


    【解决方案1】:

    这是设计使然,但我在文档中找不到任何明确说明它的参考。在满足验证标准之前,Angular 不会更改您的模型。您可以通过在输入 (here) 上方添加 {{user.password}} 来看到这一点。在您输入 8 个字符之前,您不会看到页面上的文本。

    您可以通过使用手动同步两个文本字段的指令来解决此问题,如下所示:

    http://jsfiddle.net/langdonx/K6Qgm/11/

    HTML

    <div ng-app="app" ng-controller="x">
        password is: {{user.password}}
        <br/>
        <br/>
        standard input:
        <input ng-model="user.password" name="uPassword" type="password" ng-hide="isChecked" ng-minlength="8" ng-maxlength="20" placeholder="Password" required/>
        <br/>
        <br/>
        password directive: 
        <password ng-model="user.password" name="uPassword" />
    </div>
    

    JavaScript

    function x($scope) {
        $scope.user = {
            password: 'x'
        };
    }
    
    angular.module('app', [])
        .directive('password', function () {
        return {
            template: '' +
                '<div>' +
                '    <input type="text" ng-model="ngModel" name="name" ng-minlength="8" ng-maxlength="20" required />' +
                '    <input type="password" ng-model="ngModel" name="name" ng-minlength="ngMinlength" required />' +
                '    <input type="checkbox" ng-model="viewPasswordCheckbox" />' +
                '</div>',
            restrict: 'E',
            replace: true,
            scope: {
                ngModel: '=',
                name: '='
            },
            link: function (scope, element, attrs) {
                scope.$watch('viewPasswordCheckbox', function (newValue) {
                    var show = newValue ? 1 : 0,
                        hide = newValue ? 0 : 1,
                        inputs = element.find('input');
                    inputs[show].value = inputs[hide].value;
                    inputs[hide].style.display = 'none';
                    inputs[show].style.display = '';
                });
            }
        };
    });
    

    【讨论】:

    • 感谢您的宝贵时间和回复 ;) 会试试这个
    • 我实际上仍在玩弄它——我似乎无法在指令内部进行表单验证。它似乎不太适合这种形式。
    • 奇怪的是他没有在他的范围内持有值,我想我会删除 ng-minlength 并自己控制值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多