Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS equivalent for Angular ng-container

Is here an AngularJS equivalent for Angular ng-container?

Or should I create something myself with a transclude directive?


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.

  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.

  3. Table-like arrangement of data with 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.

like image 904
mvermand Avatar asked Apr 19 '18 10:04

mvermand


People also ask

What is Ng include in AngularJS?

AngularJS ng-include Directive The ng-include directive includes HTML from an external file. The included content will be included as childnodes of the specified element. The value of the ng-include attribute can also be an expression, returning a filename.

What is container in AngularJS?

The panel directive is an example of a container. Panel takes a title attribute from the view, uses @ in the isolate scope to insert it into the template, and uses transclusion to append a clock component from the view into the template after the alert-box title.

What can I use instead of NG repeat?

You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

What is ng-template and Ng-container in Angular?

ng-container serves as a container for elements which can also accept structural directives but is not rendered to the DOM, while ng-template allows you to create template content that is not rendered until you specifically (conditionally or directly) add it to the DOM.


3 Answers

In cases you have mentioned, you can use ng-repeat-start and ng-repeat-end pair, respectively:

  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:

    <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

like image 106
Paweł Żurowski Avatar answered Oct 19 '22 15:10

Paweł Żurowski


Unfortunately in AngularJS ng-repeat, ng-if, etc must be used on an HTML element that will be present in the markup. Depending on the exact use-case you could try using an element directive with replace: true.

like image 44
ach Avatar answered Oct 19 '22 17:10

ach


You can use the same ng-container in both Angularjs and Angular-2 as well. But in angular-2 have an additional option is ng-template which is act as ng-container

like image 1
Ramesh Rajendran Avatar answered Oct 19 '22 17:10

Ramesh Rajendran