【问题标题】:Issue dynamically generating a form using angularJS使用 angularJS 动态生成表单的问题
【发布时间】:2014-07-17 16:05:22
【问题描述】:

我正在尝试使用 angularJS 动态生成表单。 通常这很好用,但是这种形式有动态类型,一些应该是文本、电子邮件,重要的是密码。 真的我希望它适用于一切,但密码是最重要的。

目前我的代码是

<input ng-repeat="input in credForm.loginForm.fields" type="{{input.fieldType}}"
ng-model="credForm.loginForm.fields[$index].value" ng-model-options="{ updateOn:
'blur' }" placeholder="{{input.fieldName}}..." ng-required="!input.optional">

这一切都很好,但是类型不起作用我收到错误“错误:类型属性无法更改”,这显然是一个安全问题。我们如何通过在 javascript 中生成一个元素添加添加它作为具有 ID 的子元素来以有角度的方式解决这个问题。

【问题讨论】:

    标签: angularjs input angularjs-ng-repeat dynamically-generated ng-repeat


    【解决方案1】:

    这是我正在使用的指令中的处理方式——注意链接函数底部的编译。在您的情况下,您可能需要一个仅针对该类型的指令 - 这样您就可以继续使用现有代码 - 类似于 field-type="{{input.fieldType}}",然后只需使用 element.find("input").attr("type", attrs.fieldType),然后进行编译。

      angular.module("schemaForm").directive("schemaFormField", 
        ['$compile', '$templateCache', '$dialog', '$q', 
        function ($compile, $templateCache, $dialog, $q) {
            return {
                restrict: "EA",
                replace: true,
                require: "^form",
                scope: { 
                    schema: "=",
                    model: "=",
                    field: "=",
                    required: "=",
                    onClick: "@",
                    editAs: "@"
                },
                link: function (scope, element, attrs, formController) {
                    var template;
                    var searchBox = $templateCache.get("searchBox.html");
    
                    scope.formState = formController; // form state checking (pristine, etc.)
    
                    scope.opts = {
                        backdrop: true,
                        keyboard: true,
                        backdropClick: true,
                        template: searchBox, 
                        //templateUrl: 'schemaFormSearchBox.html',
                        controller: 'schemaFormSearchCtrl'
                    };
    
                    // enables ng-show of label                
                    scope.isEmpty = function() { 
                      return typeof scope.model[scope.field] === 'undefined';
                    };
    
                    // launches search dialog on ng-click
                    scope.search = function() {
                      scope.opts.item = scope.schema;
                      var d = $dialog.dialog(scope.opts);
                      d.open().then(function (result) {
                        if (result) { // don't overwrite if cancelled
                          scope.model[scope.field] = result;
                        }
                      });
                    };
    
                    // perhaps throw exeception instead?
                    if (scope.schema === undefined) {
                      return $compile(element.contents())(scope);
                    }
    
                    // assigned template according to form field type
                    template = (scope.schema["enum"] !== undefined) && 
                               (scope.schema["enum"] !== null) ? 
                               $templateCache.get("enumField.html") : 
                               $templateCache.get("" + scope.schema.type + "Field.html");
                    element.html(template);
    
                    // update attributes - type, ng-required, ng-pattern, name
                    if (scope.schema.type === "number" || scope.schema.type === "integer") {
                        element.find("input").attr("type", "number");
                    }
                    element.find("input").attr("ng-required", scope.required);
                    if (scope.schema.pattern) {
                        element.find("input").attr("ng-pattern", "/" + scope.schema.pattern + "/");
                    }
                    element.find("input").attr("name", scope.field);
    
                    // compile template against current scope
                    return $compile(element.contents())(scope);
                }
            };
        }]);
    })
    

    【讨论】:

    • 如果您需要帮助来制作仅限于设置类型的指令,请添加评论,我会发布一些详细的代码。
    • 我猜这将是角度方式,自定义指令生成具有动态类型的输入标签作为模板。我需要出去,但如果我回来时没有收到更好的答案,我会标记答案。
    • 所以我深入研究了一些代码,实际上发现 ...显然它是可能的。但是我一辈子都找不到 hh 是什么...我可以访问所有代码,并且我在所有文件中搜索 hh 1730 次命中 90% 与日期格式和一些命中有关编码文件,在模板中使用,但我在哪里看不到它被定义为指令或任何类似的东西。
    • 我不知道 hh 是什么,但是让 ng-repeat 输入语句动态分配类型是有意义的——因为它将由 ngRepeat 指令编译。当然,如果你走这条路,你不能轻易地用标签、跨度和帮助块等东西来包装你的字段。
    • 我也不知道 hh 是什么,但目前我假设它是某种东西,并且已被部分删除。它应该工作但它对我不起作用是有道理的,似乎 ng-repeat 可能会将 添加到 dom 然后尝试将类型从默认(“文本”)更改为“密码”而不是编译 直接。我将设置一个小提琴来测试更详细的庄园。
    猜你喜欢
    • 2015-12-16
    • 2015-07-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多