【问题标题】:Directive generate directive field dynamically does not compile指令生成指令字段动态不编译
【发布时间】:2023-03-15 18:47:02
【问题描述】:

我创建了名为 field 的基本 HTML 字段生成器指令,如下所示

app.directive('field',function(){
        return {
            restrict : "E",
            scope : {
               fielddata : '='
            },
            link : function(scope,elem,attr){
                var content;
                scope.Options = {
                    id: scope.$id+'_'+elem.attr('id'),
                    label : elem.attr('label'),
                    placeholder : elem.attr("placeholder"),

                };
                scope.contentUrl = 'templates/fields/'+elem.attr('template')+'.html';           
            },
            template: '<div ng-include="contentUrl"></div>'
        }
    }) 

我可以生成如下所示的字段

<field id="NAME" template="text" label="First Name" placeholder="Enter First Name" fieldData="NAME"></field>

查看plunker

扩展功能以生成元素指令本身。有一个简单的对象来设置 field 元素并使用另一个指令来生成 元素指令

控制器

app.controller('contactController', ['$scope', 'dataService',function($scope,dataService) {
        $scope.message = 'Contact us! . This is just a demo.';
        dataService.getContactData().then(function(data) {
            $scope.NAME = data.first_name;
            $scope.LNAME = data.last_name;
            $scope.DESC = data.description;
        });
          $scope.fields= [
            {
              elements: [
                            {
                                template_name : "text"
                                id: "NAME",
                                label: "First Name",
                                placeholder : "First Name"
                            }           
                        ],
              html: '<div ng-repeat="elem in t.elements"><field id="{{elem.id}}" template="{{elem.template_name}}" label="{{elem.label}}"  placeholder="{{elem.placeholder}}""  fieldData="{{elem.id}}" ></field></div>'
            }
          ];
    }]);
    app.directive("bindCompiledHtml", function($compile, $timeout) {
      return {
        template: '<div></div>',
        scope: {
          rawHtml: '=bindCompiledHtml'
        },
        link: function(scope, elem, attrs) {
          scope.$watch('rawHtml', function(value) {
            if (!value) return;
            var newElem = $compile(value)(scope.$parent);
            elem.contents().remove();
            elem.append(newElem);
          });
        }
      };
    });
});

说contact.html

<div ng-repeat="t in fields" bind-compiled-html="t.html"></div>

我的问题是,bindCompiledHtml 指令生成元素并按预期调用 field 指令,但它的值未被解析。从字段指令链接函数设置断点给出 elem 如下

[
<field id=​"{{elem.id}​}​" template=​"{{elem.template_name}​}​" label=​"{{elem.label}​}​" placeholder=​"{{elem.placeholder}​}​" class=​"ng-isolate-scope">​
<!-- ngInclude: contentUrl -->
</field>​
]

控制台给出以下错误

/templates/fields/%7B%7Belem.template_name%7D%7D.html 404 (Not Found) 

要查看 404 请检查此plunker 控制台

当 field 指令的所有值都被隔离范围解析后,我们如何调用它?请指教

【问题讨论】:

  • 您能具体说明您要解决的初始问题吗?您是否试图让页面具有动态内容,您可以在其中“打开/关闭”在多个模板中定义的带有参数的块?
  • @KirillSlatin 我正在尝试使用自定义指令生成字段。最初我尝试使用基于 指令 attrs 呈现动态 HTML 字段的字段指令。现在我想使用“bindCompiledHtml”生成动态字段指令本身,在这里它生成动态 并在解析之前调用 指令.since value is not get parsed , template url go 404.please advice
  • @KirillSlatin 你得到问题了吗?
  • plunker 代码的预期行为是什么?
  • @KirillSlatin 目标是 bindCompiledHtml 指令应该生成 元素,然后通过字段指令转换文本框。但现在失败

标签: javascript jquery html angularjs


【解决方案1】:

Dinesh,没有给定的原始问题,您的代码相当复杂。这就是为什么我要询问这些嵌套编译的目的。

您在当前代码中遇到的麻烦是指令的定义。一旦它的属性没有通过= 在隔离范围内定义,它们的值将永远不会被评估,并且将始终保持纯文本。这就是为什么当您为 field 指令的属性定义值时,不能在 ng-repeat 中使用表达式,例如 {{t.template_name}}。指令应具有以下实现

app.directive('field', function() {
  return {
    restrict: "E",
    scope: {
      placeholder: '=',
      label: '=',
      id: '=',
      template: '='
    },
    link: function(scope, elem, attr) {
      var content;
      scope.Options = {
        id: scope.$id + '_' + scope.id,
        label: scope.label,
        placeholder: scope.placeholder
      };
      scope.contentUrl = scope.template + '.html';
    },
    template: '<div ng-include="contentUrl"></div>'
  }
});

因此,您将总是必须为这些属性定义一个表达式。因此,在“手动”标记中,您将使用 "'my text'" 而不是 "my text"

<field id="NAME" template="'text'" label="'First Name'" 
     max-size="40" placeholder="'Enter First Name'" 
     type="edit" ></field>

更新plunker

【讨论】:

  • 来自 bindCompiledHtml 指令 fieldData="elem.id" 将其 id 作为值。从我的控制器工厂服务中,我分配了 $scope.NAME = data.first_name;它不会预先填充 ajax 数据,而是预先填充其 id 作为值。
  • 检查这个 plunker plnkr.co/edit/3YdxamEHNGKGKQGi7ypJ?p=previewbindCompiledHtml 指令应该从 json 获取数据,而不是显示 ID
  • 你为什么不问一个新问题?在这种情况下,很多人将能够看到它
猜你喜欢
  • 2022-12-06
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 2019-11-06
相关资源
最近更新 更多