I'm getting an output boolean value from a child component in html. Now I need to use the same value to hide that same tag where I'm getting it.
<!-- where I get the value. -->
<app-pcomponent (outageEvent)='receiveEvent($event)'></app-pcomponent>
...
receiveEvent($event) {
this.value = $event as boolean;
}
I need to use the same value ie $event/this.outage
to show or hide the same component <app-pcomponent>
.
just a short recap: Your child component has a async call. The result of that call decides if the child should be shown.
There are quite a few things that are relevant:
The code in a component (in ngOnInit or in the constructor) will only be executed when the component is shown at least ones.
If you subscribe in a component some observable, it is a best practice to unsubscribe later (at least, when the component is destroyed). This is to safe guard yourself from memory leaks and "funny" behavior. Because if you don´t do that, and the user proceeds to some completly different pages in your application, then the subscription will STILL kick in. That means, that your async call (to the backend ?) will still be executed every few minutes.
My personal solution would be to create two components. The first child will handle the async call. It will (depending on the result of the call) show the second component. Thats the one with all the nice content.
As a result nothing from my components leak outside to the outer parent. My First component has only the task to handle the call and depending on the result show the child (the second component). If the second component needs data from the async call, it will be provided by a @Input() into the second component.
My personal best practice: Components are cheap, use them to seperate your code, make that way clean and maintainable.
warm regards Jan
You can use *ngIf
<app-pcomponent (outageEvent)='receiveEvent($event)' *ngIf="value"></app-pcomponent>
receiveEvent(value: boolean) {
this.value = value;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.