I want to show and hide an element by using a button that\'s located in another component.
So I have a Dashboard with an amount of chambers in it and a header.
There was a small mismatch between the @Input() kamers
and the template *ngIf="kamer.patient == null"
.
kamer
.
{{kamer.id}}
@Component({
selector: 'material-app',
templateUrl: 'app.component.html'
})
export class AppComponent {
kamer = {
patient: null,
id: '1'
};
showId = false;
ngOnInit() {
}
toggleId() {
this.showId = !this.showId;
}
}
@ViewChild
To reference a child component's functions use ViewChild
@ViewChild('child') child;
in the parent component
@Component({
selector: 'material-app',
template: `
`
})
export class AppComponent {
@ViewChild('child') child;
}
{{kamer.id}}
@Component({
selector: 'material-child',
templateUrl: 'app.component.html'
})
export class ChildComponent {
kamer = {
patient: null,
id: '1'
};
showId = false;
ngOnInit() {
}
toggleId() {
this.showId = !this.showId;
}
}
@Output()
+ EventEmitter
To extend the previous setup to use a sibling component you can
ViewChild
@Component({
selector: 'material-sibling',
template: `
`
})
export class SiblingComponent {
@Output() toggle = new EventEmitter();
}
@Component({
selector: 'material-app',
template: `
`
})
export class AppComponent {
@ViewChild('child') child;
}
{{kamer.id}}
@Component({
selector: 'material-child',
templateUrl: 'app.component.html'
})
export class ChildComponent {
kamer = {
patient: null,
id: '1'
};
showId = false;
ngOnInit() {
}
toggleId() {
this.showId = !this.showId;
}
}