简体   繁体   English

AngularJS等效于Angular ng-container

[英]AngularJS equivalent for Angular ng-container

Is here an AngularJS equivalent for Angular ng-container ? 这是一个AngularJS等效的Angular ng-container

Or should I create something myself with a transclude directive? 或者我应该用transclude指令自己创建一些东西?


Example use cases: 用例示例:

  1. Tables with interlaced pairs, triples etc. of cells: 具有隔行对,三元组等的表格:

     <table><tr> <ng-container ng-repeat="item in items"> <td>{{ item.a }}</td> <td>{{ item.b }}</td> </ng-container> </tr></table> 

    There should not be additional level in the DOM tree, and instead <td> s should be direct children of <tr> element. DOM树中不应该有额外的级别,而<td> s应该是<tr>元素的直接子级。

  2. There is similar problem with HTML lists, especially the definiton list where <dt> / <dd> elements should be direct children of <dl> while they usually exist in pairs. HTML列表存在类似的问题,尤其是定义列表,其中<dt> / <dd>元素应该是<dl>直接子元素,而它们通常成对存在。

  3. Table-like arrangement of data with display:grid : 具有display:grid的数据表格式排列display:grid

     <div style="display: grid; grid-template-columns: auto auto"> <ng-container ng-repeat="item in items"> <div>{{ item.a }}</div> <div>{{ item.b }}</div> </ng-container> </div> 

    This case is even more problematic as the container element existing in DOM tree causes the whole layout to break, as it is rendered as the cell instead of its children. 这种情况甚至更成问题,因为DOM树中存在的容器元素导致整个布局中断,因为它被渲染为单元而不是其子元素。

In cases you have mentioned, you can use ng-repeat-start and ng-repeat-end pair, respectively: 在您提到的情况下,您可以分别使用ng-repeat-startng-repeat-end对:

  1. Tables with interlaced pairs, triples etc. of cells: 具有隔行对,三元组等的表格:

     <table><tr> <td ng-repeat-start="item in items">{{ item.a }}</td> <td ng-repeat-end>{{ item.b }}</td> </tr></table> 
  2. HTML lists: HTML列表:

     <dl> <dt ng-repeat-start="item in items">{{ item.term }}</dt> <dd ng-repeat-end>{{ item.definition }}</dd> </dl> 
  3. Table-like arrangement of data with display:grid: 具有显示的数据表格式排列:网格:

     <div style="display: grid; grid-template-columns: auto auto"> <div ng-repeat-start="item in items">{{ item.a }}</div> <div>{{ item.b }}</div> <div ng-repeat-end>{{ item.c }}</div> </div> 

You can read about this in API reference: https://docs.angularjs.org/api/ng/directive/ngRepeat#special-repeat-start-and-end-points 您可以在API参考中阅读此内容: https//docs.angularjs.org/api/ng/directive/ngRepeat#special-repeat-start-and-end-points

You can use the same ng-container in both Angularjs and Angular-2 as well. 您也可以在AngularjsAngular-2使用相同的ng-container But in angular-2 have an additional option is ng-template which is act as ng-container 但是在angular-2有一个额外的选项是ng-template ,它充当了ng-container

Unfortunately in AngularJS ng-repeat , ng-if , etc must be used on an HTML element that will be present in the markup. 不幸的是,在AngularJS ng-repeat ,必须在标记中出现的HTML元素上使用ng-if等。 Depending on the exact use-case you could try using an element directive with replace: true . 根据确切的用例,您可以尝试使用带有replace: true的元素指令。

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

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