2016-05-03 94 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>