简体   繁体   English

使用列表动态构建ng-template

[英]dynamically Build ng-template using list

I am trying to build ng-template dynamically, In the Data structure, I have a Object that contains list of object of same type or other type of object. 我正在尝试动态构建ng-template,在Data结构中,我有一个Object,其中包含相同类型或其他类型的对象的列表。

[
                      {
                            type: "device",
                            id: 1,
                            allowedTypes: ["device","action"],
                            max: 5,
                            columns: [

                                {
                                    type: "action",
                                    id: "1"
                                },
                                {
                                      type: "device",
                                      id: 2,
                                      allowedTypes: ["device","action"],
                                      max: 5,
                                      columns: [

                                          {
                                              type: "action",
                                              id: "1"
                                          },
                                          {
                                              type: "action",
                                              id: "2"
                                          }

                                      ]
                                  }

                            ]
                        }
]

My ng-template code: 我的ng-template代码:

<script type="text/ng-template" id="and.html">
       <div class="container-element box box-red">
           <h3>And {{item.id}}</h3>

           <ul dnd-list="item.columns"
                 dnd-allowed-types="item.allowedTypes"
                   dnd-disable-if="item.columns.length >= item.max">


               <li ng-repeat="element in item.columns"
                   dnd-draggable="element"
                   dnd-effect-allowed="move"
                   dnd-moved="item.columns.splice($index, 1)"
                   dnd-selected="models.selected = element"
                   ng-class="{selected: models.selected === element}"
                   ng-include="element.type + '.html'" onload="onloadAction()">
               </li>
           </ul>

           <div class="clearfix"></div>
       </div>
   </script>

In the ng-template code the first level or different types of object can easily be added, however same type of object does not work properly when added in the 2nd level, it seems the its going in a recursive loop. 在ng-template代码中,可以很容易地添加第一级或不同类型的对象,但是在第二级中添加相同类型的对象不能正常工作,这似乎是在递归循环中进行。 This is because the nested ng-template (child) ng-template use of root item.columns from the ng-repeat="element in item.columns". 这是因为嵌套的ng-template(子ng)模板使用了来自item.columns中的ng-repeat =“ element”的根item.columns。

               <li ng-repeat="element in item.columns"
                   dnd-draggable="element"
                   ng-include="element.type + '.html'">
               </li>

Ng模板嵌套

any advise how to resolve this issue. 任何建议如何解决此问题。

I have managed to resolve this issue, ng-repeat create a new child scope, however the item from the parent scope is also visible to in the child scope, as this is recursive that mean child template and parent template both have the variable with the same name called item . 我设法解决了这个问题,ng-repeat创建一个新的子范围,但是父范围中的在子范围中也是可见的,因为这是递归的,这意味着子模板和父模板都具有带有名字叫item However the child scope variable item is ignored: 但是,子范围变量项将被忽略:

           <li ng-repeat="element in item.columns"
               dnd-draggable="element"
               ng-include="element.type + '.html'">
           </li>

in the above snippet the element also has a variable called item, so in the child scope when we call item.columns the item variable of parent is invoked again, to ensure that the item variable of child is not ignored, we need to use ng-init with ng-include and set the current element as item in the child scope as shown below: 在上面的代码片段中,元素还具有一个名为item的变量,因此在子范围中,当我们调用item.columns时,再次调用parent的item变量,以确保不忽略child的item变量,我们需要使用ng使用ng-include -init并将当前元素设置为子作用域中的item ,如下所示:

           <li ng-repeat="element in item.columns"
               dnd-draggable="element"
               ng-include="element.type + '.html'" ng-init="item=element">
           </li>

To read more on this please visit http://benfoster.io/blog/angularjs-recursive-templates 要了解更多信息,请访问http://benfoster.io/blog/angularjs-recursive-templates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM