angularjs - 具有动态生成的输入字段的 Angular 指令无法显示验证

标签 angularjs validation angularjs-directive angularjs-scope angular-template

经过 3 天的搜寻 stackoverflow 和其他网站后,我发现自己又回到了原点。

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

HTML:

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

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={};
}

指令:

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:'=?'
    }
};
});

显然存在一些范围问题,因为我无法访问指令外部的表单字段,也无法访问指令内部的表单名称。我也知道 $scope.myForm.name 属性不能是 Angular 绑定(bind)表达式,但我不确定如何重写它以使其起作用。

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

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

最佳答案

在调试问题时,我发现名称属性未正确编译为表单。它显示{{content.uniqueId}}名义上但实际上它在 UI 上正确呈现。

例如。 对于下面的 html。

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

名称呈现为 name="your_name_0"但在表单集合中它显示 {{content.uniqueId}}与插值指令。

似乎名称未正确插入。

然后发现issue使用 AngularJS,“您无法动态设置名称属性以进行表单验证。”

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

&如果你想在里面使用它们ng-repeat ,那么您应该始终使用嵌套 ng-form 。成员(member)里面ng-repeat将有自己的形式,并且使用该内部形式您可以处理您的验证。 Link For Reference

代码更改

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>'
    };

只是我改变了模板html,基本上添加了<ng-form></ng-form>模板并在内部表单的基础上处理验证。

这是您的Working Fiddle

希望这已经澄清了您的理解。谢谢。

关于angularjs - 具有动态生成的输入字段的 Angular 指令无法显示验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28118134/

相关文章:

javascript - 一次显示一个隐藏指令 - AngularJS

angularjs - 如何使用 angularJs 显示从 Restful api 获取的图像?

javascript - 将 Json 从 Angular 发送到 php 文件

validation - 使用训练阶段使用的完全相同的数据集进行交叉验证是否合适?

java - 随着应用程序的增长,Struts 2 验证无法正常工作

javascript - Angular UI 网格 : render image if it exist on a separate server otherwise set a default image

javascript - 如何使用 angularJS 显示从 json 获取的完整路径 url 的图像?

angularjs - 避免对 Angular JS 的 XSS 攻击

c - 如何确保用户输入是唯一的

regex - 如何使用 ng-pattern 验证 angularJs 中的电子邮件 ID