NgSwitch Directives
ngSwitch
is actually comprised of two directives, an attribute directive and a structural directive. It's very similar to a switch statement in JavaScript and other programming languages, but in the template.
Here we see the NgSwitch
attribute directive (ngSwitch
) being attached to an element. This expression bound to the directive defines what will compared against in the switch structural directives. If an expression bound to ngSwitchCase
matches the one given to ngSwitch
, those components are created and the others destroyed. If none of the cases match, then components that have ngSwitchDefault
bound to them will be created and the others destroyed. Note that multiple components can be matched using ngSwitchCase
and in those cases all matching components will be created. Since components are created or destroyed, be aware of the costs in doing so.
Last updated