ng2 - Difference between ng-container and ng-template tags

前端 未结 7 975
温柔的废话
温柔的废话 2020-12-01 00:15

Can someone please illustrate the difference between using and elements?

I could not find document

7条回答
  •  失恋的感觉
    2020-12-01 01:10

    ng-template
    The is an Angular element for rendering HTML. It is never displayed directly. Use for structural directives such as: ngIf, ngFor, ngSwitch,..
    Example:

    {{hero.name}}

    Angular translates the *ngIf attribute into a element, wrapped around the host element, like this.

    
      
    {{hero.name}}

    ng-container
    Use as a grouping element when there is no suitable host element.
    Example:

    Pick your favorite hero ()

    This will not work. Because some HTML elements require all immediate children to be of a specific type. For example, the show sad)

    This will work.

    More information: Angular Structural Directive

提交回复
热议问题