我想通過使用位於另一個組件中的按鈕來顯示和隱藏元素。Angular 2隱藏和顯示元素使用* ngIf與布爾值
所以我有一個儀表板,其中有一定數量的腔室和一個頭部。
HTML DashboardComponent與應用頭和應用室:
<app-header></app-header>
<div class="container">
<div class="row">
<app-chamber [kamers]="kamers"></app-chamber>
</div>
</div>
我有這樣的HTML問心無愧的* ngIf在我ChamberComponent:
<div class="col-sm-6 col-md-4 col-lg-3 cardcol" *ngFor="let kamer of kamers; let i = index">
<md-card class="chamber wit" *ngIf="kamer.patient == null">
<p *ngIf="showId">{{kamer.id}}</p>
</md-card>
</div>
在HeaderComponent我有一個按鈕,需要顯示和隱藏元素:
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
@Input() aList;
dashboardComponent:DashboardComponent;
chamberComponent:ChamberComponent;
constructor(dashboardComponent: DashboardComponent, chamberComponent:ChamberComponent) {
this.dashboardComponent = dashboardComponent;
this.chamberComponent = chamberComponent;
}
ngOnInit() {
}
// THIS GETS CALLED BY A BUTTON CLICK
toggleId(){
this.chamberComponent.toggleId();
}
}
然後我ChamberComponent代碼:
@Component({
selector: 'app-chamber',
templateUrl: './chamber.component.html',
styleUrls: ['./chamber.component.css']
})
export class ChamberComponent implements OnInit {
@Input() kamers;
showId:boolean;
constructor() {
this.showId=false;
}
ngOnInit() {
}
toggleId =() => {
this.showId = !this.showId;
}
}
所以,當我按一下按鈕,價值的變化(我在控制檯登錄此),但認爲沒有更新..
當我把一個按鈕,在我的ChamberComponent調用toggleId()函數視圖不會引起更新,並且元素被顯示或隱藏。
但我需要從我的標題上的按鈕來切換它。
的可能的複製[角2顯示和隱藏的元素](http://stackoverflow.com/questions/35163009/angular-2-show-and-hide-an-element) –
的重複在回調範圍內關於'this'。不過,我的代碼應該按照這個例子工作。 this.showId not working .. – Sytham
如果它沒有顯示或隱藏,並且你沒有其他內容,你確定符合'* ngIf =「kamer.patient == null」'的條件嗎? – Alex