2016-05-03 96 views
10

演示http://plnkr.co/edit/7uoVecfa62i8No8GtQHI?p=preview角2 - ngOnDestroy未被触发

当我隐藏使用*每嵌套组件的ngIf,ngOnDestroy被触发嵌套组件的第一部分。

<div *ngIf="!ff2"> 
    <my-component 
    ></my-component> 
    <my-component 
    ></my-component> 
    <my-component 
    ></my-component> 
    <my-component 
    ></my-component> 
    <my-component 
    ></my-component> 
    </div> 

输出在控制台:

init 
    init 
    init 
    init 
    init 
    destroy 
    destroy 
    destroy 
    destroy 
    destroy 

但是,当我隐藏第二部分,在那里子由* ngFor复制,不是每个ngOnDestroy被触发。

<div *ngIf="!ff"> 
     <my-component 
      *ngFor="#i of [1,2,3,4,5,6]" 
     ></my-component> 
     </div> 

输出在控制台:

(6) init 
(3) destroy 

你有什么想法,如果我做错了什么,或者有一个问题angular2?谢谢。

+1

看起来像一个错误。 –

+2

在角度测试9它工作正如我所料,所以他们有一个错误 http://plnkr.co/edit/Q8tLJKlpF6wEVcMWfxH1?p=preview –

+0

错误报告https://github.com/angular/angular/issues/8458 –

回答

1

请尝试下面提到的代码。它应该工作,

<button type="button" (click)="ff = !ff">toggle</button> 
<template ngFor let-item [ngForOf]="[1,2,3,4,5,6]"> 
    <my-component *ngIf="!ff"></my-component> 
</template>