2016-08-04 6 views
0

ngIf属性をtrueと評価した後でngIf属性を削除する方法はありますか?ngIf属性がtrueと評価された後でngIf属性を削除します

の例では、私は

<div class="container"> 
    <div *ngFor="let el of elements"> 
     <div *ngIf="el.index = index"> 
     ... 
     </div> 
    </div> 
</div> 

class Test implements onInit{ 
    index = 0; 

    ngOnInit(){ 
    setInterval(() => index++) 
    } 
} 
これは一度に一つだけのdivが表示されることを意味する

、しかし私は、すべてのdivをカウンタをしたいし、コンポーネントに私が持っているテンプレートを持って言うことができますそれ以前に表示されていたものは、他の言葉で表示されたままになり、ngIfプロパティがtrueと評価されると削除されます。

可能なことはありますか?

ありがとうございました

答えて

1

これはできません。 *ngForがレンダリングする内容を含めるために、コンポーネント内の要素のリストを維持する必要があります。

+0

ですか? – Darlyn

+0

http://stackoverflow.com/questions/36427670/angular2-calling-custom-function-after-ngswitch-new-view-is-created/36427769#36427769のような指令を使用することができますが、それよりむしろAngular2(おそらくjQueryウィジェットを使用していることを除いて) –

+0

isViewed属性がtrueかどうかを自動的に返すかどうかをチェックする関数を渡して解決しました。条件をチェックしていない場合、条件が真であればisViewedをtrueに設定します – Darlyn

0

独自ngIfを作成することができます。

@Directive({ selector : '[myNgIf]' }) 
export class myNgIfDirective { 
    private condition; 

    constructor (private templateRef : TemplateRef<any> , 
        private viewContainer : ViewContainerRef) { 
    } 

    @Input() set myNgIf (condition : boolean) { 
     if (this.condition !== true) { 
     // if this condition is once set to true , don't bother doing anything anymore 
      this.condition = condition; 
      if (condition) { 
       this.viewContainer.createEmbeddedView(this.templateRef); 
      } else { 
       this.viewContainer.clear(); 
      } 
     } 
    } 
} 

は、あなたがそれを使用することができます:それはngIfが真の解決時の機能を評価するために、少なくとも可能

<div *myNgIf ="isTrue">stuff</div> 
関連する問題