I want to show and hide an element using a button located in another component.
So, I have a toolbar with the number of cameras in it and a title.
HTML DashboardComponent with application header and application camera:
<app-header></app-header>
<div class="container">
<div class="row">
<app-chamber [kamers]="kamers"></app-chamber>
</div>
</div>
I have this HTML wth * ngIf in my 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>
In the HeaderComponent, I have a button that should show and hide the element:
@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() {
}
toggleId(){
this.chamberComponent.toggleId();
}
}
then my ChamberComponent code:
@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;
}
}
Therefore, when I click the button, the value changes (I registered this in the console), but the view is not updated.
When I put a button in my ChamberComponent, which calls the toggleId () function, the view shows the update and the item is displayed or hidden.
But I need to switch it from the button on my header.