角指令与动态生成的输入域不能显示验证

时间:2023-01-28 22:30:30

After 3 days of scouring * and other sites, I have found myself back at square one.

经过3天对*和其他网站的搜索,我发现自己又回到了起点。

My task: I need to validate dynamically generated form fields.

我的任务是:我需要验证动态生成的表单字段。

The HTML:

HTML:

 <form name="myForm">
    <form-field content="field" model="output[field.uniqueId]" ng-repeat="field in formFields"></form-field>
 </form>

The controller:

控制器:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
$scope.formFields = [
    {
    "fieldName": "Your Name",
    "uniqueId": "your_name_0",
    "fieldType": "text",
    "isMandatory": true
    },
    {
    "fieldName": "Description",
    "uniqueId": "description_1",
    "fieldType": "textarea",
    "isMandatory": true,
    }
];

$scope.output={};
}

The directive:

该指令:

myApp.directive("formField",function($compile){
var templates = {
    textTemplate:'<div class="form-group"><label for="{{content.uniqueId}}" >{{content.fieldName}}</label> <span ng-show="content.isMandatory" class="sub_reqText">*</span><span ng-show="form.content.fieldName.$invalid">Please check this field.</span><input type="text" ng-model="model" name="{{content.uniqueId}}" class="form-control" ng-required="content.isMandatory" id="{{content.uniqueId}}"/> </div><br>',
    textareaTemplate:'<div class="form-group"><label for="{{content.uniqueId}}" >{{content.fieldName}}</label> <span ng-show="content.isMandatory" class="sub_reqText">*</span> <span ng-show="form.content.fieldName.$invalid">Please check this field.</span> <textarea ng-model="model" name="{{content.uniqueId}}" id="{{content.uniqueId}}"  class="form-control" ng-required="content.isMandatory"></textarea> </div>' 
};

var getTemplate = function(content, attrs){
    var template = {};
    template = templates[content.fieldType+"Template"];
    if(typeof template != 'undefined' && template != null) {
            return template;
        }
        else {
            return '';
        }
};


var linker = function(scope, element, attrs){        
    element.html(getTemplate(scope.content, attrs)).show();        
    $compile(element.contents())(scope);
}

return {
    restrict:"E",        
    replace:true,        
    link:linker,
    scope:{
        content:'=',
        model:'=?'
    }
};
});

There is clearly some scope issue because I cannot access the form fields outside of the directive and I cannot access the form name inside the directive. I also know $scope.myForm.name property cannot be an angular binding expression but I am not sure how to rewrite it so that it works.

显然存在一些范围问题,因为我不能访问指令之外的表单字段,也不能访问指令内部的表单名称。我也知道$scope.myForm.name属性不能是一个角绑定表达式,但是我不知道如何重写它,以便它能工作。

This is the jsfiddle: http://jsfiddle.net/scheedalla/57tt04ch/

这是jsfiddle: http://jsfiddle.net/scheedalla/57tt04ch/

Any guidance will be very useful, thank you!

任何指导都将非常有用,谢谢!

1 个解决方案

#1


2  

While debugging the problem I found that, the name attribute is not properly compiled for form. It was showing {{content.uniqueId}} in name but actually it rendered properly on UI.

在调试问题时,我发现name属性没有为表单正确编译。这是显示{ {内容。但实际上它在UI上表现得很好。

Eg. For below html.

如。下面的html。

<input type="text" ng-model="model" name="{{content.uniqueId}}" class="form-control" 
ng-required="content.isMandatory" id="{{content.uniqueId}}"/>

name rendered as name="your_name_0" but in form collection it was showing {{content.uniqueId}} with the interpolation directive.

名称呈现为name="your_name_0",但在表单集合中显示的是{内容。具有插补指令的unique eid}。

Seems like name is not interpoluted properly.

似乎名字没有被正确地插入。

Then found issue with AngularJS, "You can't set name attribute dynamically for form validation."

然后发现AngularJS有问题,“不能动态设置name属性进行表单验证。”

Note: Above mentioned issue has been fixed in Angular 1.3.(name attributes interpolates properly)

注:上述问题已在角度1.3中修正。(名称属性插入正确)

& If you wanted to work them inside ng-repeat, then you should always use nested ng-form. Members inside ng-repeat will have their own form, and using that inner form you can handle your validation. Link For Reference

&如果您想在ng-repeat中使用它们,则应该始终使用嵌套ng-form。ng-repeat中的成员将有自己的表单,并使用该内部表单来处理验证。链接供参考

CODE CHANGE

代码更改

var templates = {
        textTemplate: '<ng-form name="form">'+
    '<div class="form-group">'+
        '<label for="{{content.uniqueId}}">{{content.fieldName}}</label> '+
          '<span ng-show="content.isMandatory" class="sub_reqText">*</span>'+
          '<span ng-show="form.input.$invalid">'+
          'Please check this field.'+
          '</span>'+
        '<input type="text" ng-model="model1" name="input" class="form-control" ng-required="content.isMandatory" id="{{content.uniqueId}}" /> '+
    '</div>'+
'</ng-form>'+
'<br>',
        textareaTemplate: '<ng-form name="form">'+
    '<div class="form-group">'+
        '<label for="{{content.uniqueId}}">{{content.fieldName}}</label>'+
          '<span ng-show="content.isMandatory" class="sub_reqText">*</span> '+
          '<span ng-show="form.textarea.$invalid">Please check this field.</span>'+
          '<textarea ng-model="model" name="textarea" id="{{content.uniqueId}}" class="form-control" ng-required="content.isMandatory"></textarea>'+
    '</div>'+
'</ng-form>'
    };

Only i changed the template html, basically added <ng-form></ng-form> for templates and handled the validation on basis it in inner form.

只是修改了模板html,基本增加了 用于模板,并在内部形式的基础上进行验证。

Here is your Working Fiddle

这是你的小提琴。

Hope this have cleared your understanding. Thanks.

希望这能让你明白。谢谢。

#1


2  

While debugging the problem I found that, the name attribute is not properly compiled for form. It was showing {{content.uniqueId}} in name but actually it rendered properly on UI.

在调试问题时,我发现name属性没有为表单正确编译。这是显示{ {内容。但实际上它在UI上表现得很好。

Eg. For below html.

如。下面的html。

<input type="text" ng-model="model" name="{{content.uniqueId}}" class="form-control" 
ng-required="content.isMandatory" id="{{content.uniqueId}}"/>

name rendered as name="your_name_0" but in form collection it was showing {{content.uniqueId}} with the interpolation directive.

名称呈现为name="your_name_0",但在表单集合中显示的是{内容。具有插补指令的unique eid}。

Seems like name is not interpoluted properly.

似乎名字没有被正确地插入。

Then found issue with AngularJS, "You can't set name attribute dynamically for form validation."

然后发现AngularJS有问题,“不能动态设置name属性进行表单验证。”

Note: Above mentioned issue has been fixed in Angular 1.3.(name attributes interpolates properly)

注:上述问题已在角度1.3中修正。(名称属性插入正确)

& If you wanted to work them inside ng-repeat, then you should always use nested ng-form. Members inside ng-repeat will have their own form, and using that inner form you can handle your validation. Link For Reference

&如果您想在ng-repeat中使用它们,则应该始终使用嵌套ng-form。ng-repeat中的成员将有自己的表单,并使用该内部表单来处理验证。链接供参考

CODE CHANGE

代码更改

var templates = {
        textTemplate: '<ng-form name="form">'+
    '<div class="form-group">'+
        '<label for="{{content.uniqueId}}">{{content.fieldName}}</label> '+
          '<span ng-show="content.isMandatory" class="sub_reqText">*</span>'+
          '<span ng-show="form.input.$invalid">'+
          'Please check this field.'+
          '</span>'+
        '<input type="text" ng-model="model1" name="input" class="form-control" ng-required="content.isMandatory" id="{{content.uniqueId}}" /> '+
    '</div>'+
'</ng-form>'+
'<br>',
        textareaTemplate: '<ng-form name="form">'+
    '<div class="form-group">'+
        '<label for="{{content.uniqueId}}">{{content.fieldName}}</label>'+
          '<span ng-show="content.isMandatory" class="sub_reqText">*</span> '+
          '<span ng-show="form.textarea.$invalid">Please check this field.</span>'+
          '<textarea ng-model="model" name="textarea" id="{{content.uniqueId}}" class="form-control" ng-required="content.isMandatory"></textarea>'+
    '</div>'+
'</ng-form>'
    };

Only i changed the template html, basically added <ng-form></ng-form> for templates and handled the validation on basis it in inner form.

只是修改了模板html,基本增加了 用于模板,并在内部形式的基础上进行验证。

Here is your Working Fiddle

这是你的小提琴。

Hope this have cleared your understanding. Thanks.

希望这能让你明白。谢谢。