【问题标题】:Form element directive with error message manage AngularJS带有错误消息的表单元素指令管理 AngularJS
【发布时间】:2016-10-20 16:20:44
【问题描述】:

我正在尝试创建一个带有验证的输入指令元素。 我想管理此指令中的错误状态。

有3个文件

  • Index.html:使用该指令
  • textValid.js:包含指令代码
  • textValid.html:包含指令模板

我创建了这个指令

textValid.js

App.directive("textValid", function() {
    return {
        restrict: "E",
        templateUrl: "tpl/textValid.html",
        require: "?ngModel",

        scope: {
            name: "@",
            element: "=",
            model: "="
        }
    };
});

index.html

<form name="edit_form_ctrl.contract_edit_form" action="#" novalidate >
     <div class="row">
         <text-valid name="ncontract"model="edit_form_ctrl.contract.ncontract"
                     element="edit_form_ctrl.contract_edit_form.ncontract">
         </text-valid>
     </div>
</form>

和模板textValid.html

<input type="text" name="name" ng-model="model" class="form-control" ng-required="true" value="{{model}}" />
     <div>pristine: {{element.$pristine}}</div> <!--is always undefined-->
     <div>Invalid: {{element.$error}}</div> <!--is always undefined-->
<span class="color-red" ng-if="element.$error.required  &&!element.$pristine">
    {{curLang.field_mandatory}}
</span>

我正在尝试获取输入控制以检查 $error 和 $pristine 值,但我无法实现。

我也阅读了所有文档和书籍,但没有任何结果。

有人尝试这样做吗?

提前致谢

【问题讨论】:

    标签: javascript html angularjs validation directive


    【解决方案1】:

    如果在输入元素的 name 属性周围添加 {{double-curlies}},如下所示:

    <input type="text" name="{{name}}" ng-model="model" class="form-control" ng-required="true" />
    

    您应该可以使用$scope.form[$scope.name]text-valid内部访问ngModelController

    其他几点说明:

    1. 使用value 属性和带有ng-model 属性的元素将不起作用。

    2. 您的指令定义中有require: '?ngModel',但text-valid 元素上没有ng-model 属性。这没关系,但是除非text-valid 元素具有ng-model 属性,否则您不会得到任何ngModelController 被注入。

    编辑:

    因为text-valid 具有隔离作用域,要访问FormController,您需要 require 并绑定它:

    App.directive("textValid", function() {
        return {
            restrict: "E",
            templateUrl: "tpl/textValid.html",
            require: "^^form",
    
            scope: {
                name: "@",
                element: "=",
                model: "="
            },
            link: function($scope, elem, attr, FormCtrl){
                $scope.form = FormCtrl;
                // now you should be able to access $scope.form[$scope.name]
                // (you *might* need to put any initialization that accesses it inside a $timeout to wait for it to be rendered/bound)
            }
        };
    });
    

    在模板中,您应该可以像这样访问它:

    <div>pristine: {{form[name].$pristine}}</div>
    <div>Invalid: {{form[name].$error}}</div>
    

    【讨论】:

    • 你的意思是在指令中的链接功能?我尝试在 textValid.js 中添加链接:function($scope, element, attr, ctrl) 但 $scope.form[name] 返回错误 'cos form is null.
    • 嗯,对,text-valid 具有隔离范围。考虑到这一点,我编辑了我的答案。
    • 谢谢你!它有效....只是一个问题:这两个'^^'在form之前是什么意思?!
    • 很高兴它有效! require 中的^^ 意味着需要来自DOM 元素的祖先。 ^ in require 意味着需要来自 DOM 元素或祖先。在 Directive Definition Object 部分中的 require 下检查 Comprehensive Directive API docs
    • 谢谢大家。
    【解决方案2】:

    试试这个:

    <input type="text" name="name" ng-model="model" class="form-control" ng-required="true" value="{{model}}" />
         <div>pristine: {{form.name.$pristine}}</div> <!--is always undefined-->
         <div>Invalid: {{form.name.$error}}</div> <!--is always undefined-->
    <span class="color-red" ng-if="element.$error.required  &&!element.$pristine">
        {{curLang.field_mandatory}}
    </span>
    

    【讨论】:

    • 这在技术上是可行的,但实际上并不允许在表单中重复使用 text-valid 指令,因为会有多个名为 'name' 的输入
    • 你是对的......我错了。 {{name}} 是正确的值。所以它可以是动态的
    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多