Using Multiple Structural Directives
Sometimes we'll want to combine multiple structural directives together, like iterating using ngFor
but wanting to do an ngIf
to make sure that the value matches some or multiple conditions. Combining structural directives can lead to unexpected results however, so Angular requires that an element can only be bound to one directive at a time. To apply multiple directives we'll have to either (a) expand the sugared syntax, (b) nest template tags or use the <ng-content>
tag.
It is important to note, that like the ng-template
elemement, ng-container
elements are not rendered to the DOM, but rather their child(ren) are/is. The added benefit of using the ng-container
element is that it allows you to group sibling elements that should also have the same structural Directive without adding an additional unnecessary parent element that will get rendered to the DOM.
The previous tabs example can use ngFor
and ngSwitch
if the tab title and content is abstracted away into the component class.
Last updated