Angular - 如何防止 ngIf 在每次条件改变时销毁组件
Angular - How to prevent ngIf from destroying the component every time the condition changes
我有这个代码:
<ng-container *ngFor="let language of languages">
<app-fields-to-translate
*ngIf="language.ID === selectedLanguage.ID"
[AttributeGroupsCollapses]="AttributeGroupsCollapses"
[AttributeGroups]="AttributeGroups"
[Attributes]="Attributes"
[categoriesCollapsed]="categoriesCollapsed"
[generalCollabsed]="generalCollabsed"
[selectedLanguage]="selectedLanguage"
(CategoriesCollapsedEmmiter)="categoriesCollapsed = $event"
(CollapseGeneralEmmiter)="generalCollabsed = $event"
(AddFieldToTranslation)="AddFieldToTranslation($event)"
(AddAttrubuteToTranslation)="AddAttributeToTranslation($event)"
>
</app-fields-to-translate>
</ng-container>
并且每次selctedLanguage改变(field-to-translate)的组件被销毁
我可以阻止这个过程吗?
还是有其他方法可以做到这一点?
根据您的业务逻辑具体执行的操作,您可以使用 [hidden]
,即
[hidden]="language.ID !== selectedLanguage.ID"
我有这个代码:
<ng-container *ngFor="let language of languages">
<app-fields-to-translate
*ngIf="language.ID === selectedLanguage.ID"
[AttributeGroupsCollapses]="AttributeGroupsCollapses"
[AttributeGroups]="AttributeGroups"
[Attributes]="Attributes"
[categoriesCollapsed]="categoriesCollapsed"
[generalCollabsed]="generalCollabsed"
[selectedLanguage]="selectedLanguage"
(CategoriesCollapsedEmmiter)="categoriesCollapsed = $event"
(CollapseGeneralEmmiter)="generalCollabsed = $event"
(AddFieldToTranslation)="AddFieldToTranslation($event)"
(AddAttrubuteToTranslation)="AddAttributeToTranslation($event)"
>
</app-fields-to-translate>
</ng-container>
并且每次selctedLanguage改变(field-to-translate)的组件被销毁 我可以阻止这个过程吗? 还是有其他方法可以做到这一点?
根据您的业务逻辑具体执行的操作,您可以使用 [hidden]
,即
[hidden]="language.ID !== selectedLanguage.ID"