Angularjs |使用$ compile从指令加载html时,ng-message不显示错误消息

时间:2022-04-20 19:44:50

I am creating dynamic from through ng-repeat from json.

我正在通过json的ng-repeat创建动态。

my form.html

    <form    name="userForm" >
     <div ng-repeat="(key,box) in form.form_fields" >
    <accordion >
            <accordion-group heading="{{key}}"  is-open="true">
                <div ng-repeat="field in box">
                       <field-directive field="field">
                        </field-directive> 
               </div>       
            </accordion-group>
  </accordion>
            </div> 
    </form>

so basically i am loading each type of field (textbox/radio/dropdown/email etc) html trough loop via directive. I am loading these small HTML by using $compile and linker. Everything is working good even validation also. I am able to enable/disable submit button based on form validation except ng-message . if i not use directive like this

所以基本上我正在通过指令加载每种类型的字段(文本框/收音机/下拉/电子邮件等)html trough loop。我使用$ compile和linker加载这些小HTML。即使验证,一切都很好。我可以根据除ng-message之外的表单验证启用/禁用提交按钮。如果我不使用这样的指令

    <form name="userForm" >

 <div ng-repeat="(key,box) in form.form_fields" >
    <accordion >
            <accordion-group heading="{{key}}"  is-open="true">
                <div ng-repeat="field in box">

                        <div class="col-md-3 col-sm-6 col-xs-12" ng-if="field.field_type ==='textfield' ">
                                <md-input-container >
                                          <label>{{field.field_title}}</label>
                                          <input ng-model="field.field_value" name="{{field.field_id}}" type="text" 
                                            ng-required="true" md-maxlength="9" >


                                     <div ng-messages="userForm.{{field.field_id}}.$error">

                                        <p ng-message="maxlength">Your name is too long.</p>
                                        <p ng-message="required">Your name is required.</p>
                                    </div>
                                 </md-input-container>   
                        </div>   

                        <div class="col-md-3 col-sm-6 col-xs-12" ng-if="field.field_type ==='number' ">
                                <md-input-container >
                                          <label>{{field.field_title}}</label>
                                          <input ng-model="field.field_value" name="{{field.field_id}}" type="text" 
                                            ng-required="true" md-maxlength="9" >


                                     <div ng-messages="userForm.{{field.field_id}}.$error">

                                        <p ng-message="maxlength">Your name is too long.</p>
                                        <p ng-message="required">Your name is required.</p>
                                    </div>
                                 </md-input-container>
                        </div>



               </div>       
            </accordion-group>
  </accordion>
            </div> 
    </form>

everything is working fine including ng-message. I can see error message also. What is the issue with ng-message if i use directive . Please help.

一切都很好,包括ng-message。我也可以看到错误信息。如果我使用指令,ng-message有什么问题。请帮忙。

1 个解决方案

#1


0  

Probably late answer but you can do

可能迟到的答案,但你可以做到

var element1 = angular.element(your_html_fragment); ... logic to append element1 in somewhere

var element1 = angular.element(your_html_fragment); ...在某处附加element1的逻辑

after this

$compile(element1)(scope) in your directive this should do it.

你的指令中的$ compile(element1)(范围)应该这样做。

The sequence of angular.element and $compile is important.

angular.element和$ compile的序列很重要。

#1


0  

Probably late answer but you can do

可能迟到的答案,但你可以做到

var element1 = angular.element(your_html_fragment); ... logic to append element1 in somewhere

var element1 = angular.element(your_html_fragment); ...在某处附加element1的逻辑

after this

$compile(element1)(scope) in your directive this should do it.

你的指令中的$ compile(element1)(范围)应该这样做。

The sequence of angular.element and $compile is important.

angular.element和$ compile的序列很重要。