【发布时间】:2021-01-12 14:34:17
【问题描述】:
有什么区别
<ng-container *ngIf="flag">
<child-component></child-component>
</ng-container>
或直接
<child-component *ngIf="flag"></child-component>
那最好的做法是什么?
【问题讨论】:
-
如果 ng-container 中只有一个元素,则不应使用 ng-container。如果您在 ng-container 中有多个元素需要在
flag为真时隐藏,您应该使用第一个替代方案。 -
如果您使用单个组件,那么最好不要使用 ng-container 指令,因为您的组件将包含额外的指令。如果您有多个组件要处理,那么为了避免多个相同的情况,您可以使用 ng-directive :)
标签: angular directive angular-ng-if