我目前正在更新DOM中的值時遇到問題。angular2 - ngIf值未在DOM中更新
爲了快速解釋並儘可能簡單,我目前有兩個組件(A和B)。 B,從A繼承,用A的視圖作爲模板,而且注入其模板到A(NG-內容),因此B的觀點是這樣的:
<A>
<template>
<div *ngFor="let item of items" (click)="selected($event, item)">
<span>{{item.name}}<span>
</div>
</template>
</A>
和A的觀點是這樣的:
<ng-content select="template"></ng-content>
<div *ngIf="searching">Hey I'm looking for answer</div>
<div *ngIf="!searching">I already have my answer</div>
組件B僅一個私有變量,它是項目的數組和A的分量也具有單一的B稱爲具體方法是:
private searching: boolean = true;
selected(event, value: any): void {
event.stopPropagation();
this.searching = false; // This.searching is updated in component but not in DOM
this.selectedItem = value; // BUT this.selectedItem is updated in component AND in DOM !!
}
更改this.selectedItem工作,但this.searching不會因此不僅是「是」由於搜索總是TRUE,因此顯示。
這裏是plunkr。
有人能夠賜教嗎?謝謝。
你爲什麼使用event.stopPropagation(); ? – distante
你可以發佈整個html和組件代碼嗎? –
以下是plunkr:https://plnkr.co/edit/DP7K5Q63kEqXmQbRA98I?p=preview – NPanda