【发布时间】:2018-12-04 20:21:28
【问题描述】:
我简化了我的问题:
<div *ngIf="layout1" class="layout1">
<div class="sidebar-layout1">
some items
</div>
<child-component [something]="sth"></child-component>
</div>
<div *ngIf="!layout1" class="layout2">
<child-component [something]="sth">
<p>some content...</p>
</child-component>
</div>
我有一个父组件,它具有正常布局(layout1)和全屏布局(layout2)的可能性(在全屏模式下,子组件应该全屏)。问题是, 当我使用 *ngIf 更改布局时,子组件被销毁并生成一个新组件。我想拥有相同的实例,不要丢失子组件的重要信息,避免一些 api 调用。
有什么方法可以实现子组件不会被销毁,或者有没有比 ngIf 更好的方法?
对于父组件中的不同布局,我只需要一个子组件实例。
【问题讨论】:
-
但使用 [hidden] 仍然占用空间。相反,在 ngStyle 上设置 display:none。这只是我的假设。试一试。我需要知道“!layout1”是任何标志或布尔值吗?
-
layout1 只是一个布尔标志,我在按钮单击事件上更新
-
好的。尝试这个。再添加一个标志,例如 if(this.paraentLayout == false){ dont made change in child} 即 *ngIf == ' parentLayout == false ? classA : ClassB'
-
我将只使用一个孩子和 ngClass 跨度>
标签: angular scope components directive