How child component could control visibility of itself in the parent on child component initialization?
up vote
0
down vote
favorite
There are two interchangeable child components in the parent, which visibility is governed by the parent variable showList
<div id="parent">
<app-child-1 *ngIf="showList"></app-child-1>
<app-child-2 *ngIf="!showList"></app-child-2>
</div>
How could I control the parent variable "showList" from the child component on its initialization?
I've tried to change it with Output and BehaviorSubject, but both this options doesn't work on initialization.
angular output angular6 behaviorsubject
add a comment |
up vote
0
down vote
favorite
There are two interchangeable child components in the parent, which visibility is governed by the parent variable showList
<div id="parent">
<app-child-1 *ngIf="showList"></app-child-1>
<app-child-2 *ngIf="!showList"></app-child-2>
</div>
How could I control the parent variable "showList" from the child component on its initialization?
I've tried to change it with Output and BehaviorSubject, but both this options doesn't work on initialization.
angular output angular6 behaviorsubject
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
There are two interchangeable child components in the parent, which visibility is governed by the parent variable showList
<div id="parent">
<app-child-1 *ngIf="showList"></app-child-1>
<app-child-2 *ngIf="!showList"></app-child-2>
</div>
How could I control the parent variable "showList" from the child component on its initialization?
I've tried to change it with Output and BehaviorSubject, but both this options doesn't work on initialization.
angular output angular6 behaviorsubject
There are two interchangeable child components in the parent, which visibility is governed by the parent variable showList
<div id="parent">
<app-child-1 *ngIf="showList"></app-child-1>
<app-child-2 *ngIf="!showList"></app-child-2>
</div>
How could I control the parent variable "showList" from the child component on its initialization?
I've tried to change it with Output and BehaviorSubject, but both this options doesn't work on initialization.
angular output angular6 behaviorsubject
angular output angular6 behaviorsubject
asked Nov 9 at 16:23
urDMG
11318
11318
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
It looks like showList
is a property of the parent component. This means that the parent component can set its value. You want the child components to "tell" the parent component that they wish to be displayed? If so you must declare an event on the child component and listen to it with your parent component. I am guessing that there will be a control on the child component which causes the view to switch, so basically the child component requests to be hidden not shown.
EDIT: Since you want the child components to decide at run time which one of them is the one to display, you can't have them be hidden by *ngIf
because then if they are hidden they are not even instantiated, and they cannot run any code which causes them to reappear. In this case you need to have them all present, but hidden from view using CSS.
The event in the child component can be declared like this:
@Output() hideMe = new EventEmitter<any>();
And the listening at the parent component is done like this:
<app-child-1 [class.hidden]="!showList" (hideMe)="showList=false"></app-child-1>
<app-child-2 [class.hidden]="showList" (hideMe)="showList=true"></app-child-2>
And the accompanying CSS:
.hidden display: none;
Then in your child component logic, when it's time to emit the event you should do:
this.hideMe.emit();
Yes, I've tried this option. If this.hideMe.emit(); is called during initialization it doesn't work. I mean in the ngOnInit method or in the ngAfterViewInit
– urDMG
Nov 9 at 17:31
You don't need to emit anything during initialization. You just need to supply a default value for the parent component variableshowList
.
– Aviad P.
Nov 9 at 17:41
Probably I didn't explain correctly. I need it during initialization. Child components are interchangeable. By the default (showList=true) in the parent component, child-component-1 is displayed. But I want to change showList property during initialization from the child-component-2 if condition is satisfied. I've tried to do in the child component 2 ngOnInit() { if(condition) this.hideMe.emit(); But it doesn't work
– urDMG
Nov 10 at 5:45
I think that because the hidden component is hidden with*ngIf
it is not even there to run its code, so it can't send any messages. I'll edit my answer.
– Aviad P.
Nov 10 at 8:37
All you have to do is to emit that output from child componant and then showhide everytime you emit the function
– Talg123
Nov 10 at 8:45
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It looks like showList
is a property of the parent component. This means that the parent component can set its value. You want the child components to "tell" the parent component that they wish to be displayed? If so you must declare an event on the child component and listen to it with your parent component. I am guessing that there will be a control on the child component which causes the view to switch, so basically the child component requests to be hidden not shown.
EDIT: Since you want the child components to decide at run time which one of them is the one to display, you can't have them be hidden by *ngIf
because then if they are hidden they are not even instantiated, and they cannot run any code which causes them to reappear. In this case you need to have them all present, but hidden from view using CSS.
The event in the child component can be declared like this:
@Output() hideMe = new EventEmitter<any>();
And the listening at the parent component is done like this:
<app-child-1 [class.hidden]="!showList" (hideMe)="showList=false"></app-child-1>
<app-child-2 [class.hidden]="showList" (hideMe)="showList=true"></app-child-2>
And the accompanying CSS:
.hidden display: none;
Then in your child component logic, when it's time to emit the event you should do:
this.hideMe.emit();
Yes, I've tried this option. If this.hideMe.emit(); is called during initialization it doesn't work. I mean in the ngOnInit method or in the ngAfterViewInit
– urDMG
Nov 9 at 17:31
You don't need to emit anything during initialization. You just need to supply a default value for the parent component variableshowList
.
– Aviad P.
Nov 9 at 17:41
Probably I didn't explain correctly. I need it during initialization. Child components are interchangeable. By the default (showList=true) in the parent component, child-component-1 is displayed. But I want to change showList property during initialization from the child-component-2 if condition is satisfied. I've tried to do in the child component 2 ngOnInit() { if(condition) this.hideMe.emit(); But it doesn't work
– urDMG
Nov 10 at 5:45
I think that because the hidden component is hidden with*ngIf
it is not even there to run its code, so it can't send any messages. I'll edit my answer.
– Aviad P.
Nov 10 at 8:37
All you have to do is to emit that output from child componant and then showhide everytime you emit the function
– Talg123
Nov 10 at 8:45
|
show 2 more comments
up vote
1
down vote
accepted
It looks like showList
is a property of the parent component. This means that the parent component can set its value. You want the child components to "tell" the parent component that they wish to be displayed? If so you must declare an event on the child component and listen to it with your parent component. I am guessing that there will be a control on the child component which causes the view to switch, so basically the child component requests to be hidden not shown.
EDIT: Since you want the child components to decide at run time which one of them is the one to display, you can't have them be hidden by *ngIf
because then if they are hidden they are not even instantiated, and they cannot run any code which causes them to reappear. In this case you need to have them all present, but hidden from view using CSS.
The event in the child component can be declared like this:
@Output() hideMe = new EventEmitter<any>();
And the listening at the parent component is done like this:
<app-child-1 [class.hidden]="!showList" (hideMe)="showList=false"></app-child-1>
<app-child-2 [class.hidden]="showList" (hideMe)="showList=true"></app-child-2>
And the accompanying CSS:
.hidden display: none;
Then in your child component logic, when it's time to emit the event you should do:
this.hideMe.emit();
Yes, I've tried this option. If this.hideMe.emit(); is called during initialization it doesn't work. I mean in the ngOnInit method or in the ngAfterViewInit
– urDMG
Nov 9 at 17:31
You don't need to emit anything during initialization. You just need to supply a default value for the parent component variableshowList
.
– Aviad P.
Nov 9 at 17:41
Probably I didn't explain correctly. I need it during initialization. Child components are interchangeable. By the default (showList=true) in the parent component, child-component-1 is displayed. But I want to change showList property during initialization from the child-component-2 if condition is satisfied. I've tried to do in the child component 2 ngOnInit() { if(condition) this.hideMe.emit(); But it doesn't work
– urDMG
Nov 10 at 5:45
I think that because the hidden component is hidden with*ngIf
it is not even there to run its code, so it can't send any messages. I'll edit my answer.
– Aviad P.
Nov 10 at 8:37
All you have to do is to emit that output from child componant and then showhide everytime you emit the function
– Talg123
Nov 10 at 8:45
|
show 2 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It looks like showList
is a property of the parent component. This means that the parent component can set its value. You want the child components to "tell" the parent component that they wish to be displayed? If so you must declare an event on the child component and listen to it with your parent component. I am guessing that there will be a control on the child component which causes the view to switch, so basically the child component requests to be hidden not shown.
EDIT: Since you want the child components to decide at run time which one of them is the one to display, you can't have them be hidden by *ngIf
because then if they are hidden they are not even instantiated, and they cannot run any code which causes them to reappear. In this case you need to have them all present, but hidden from view using CSS.
The event in the child component can be declared like this:
@Output() hideMe = new EventEmitter<any>();
And the listening at the parent component is done like this:
<app-child-1 [class.hidden]="!showList" (hideMe)="showList=false"></app-child-1>
<app-child-2 [class.hidden]="showList" (hideMe)="showList=true"></app-child-2>
And the accompanying CSS:
.hidden display: none;
Then in your child component logic, when it's time to emit the event you should do:
this.hideMe.emit();
It looks like showList
is a property of the parent component. This means that the parent component can set its value. You want the child components to "tell" the parent component that they wish to be displayed? If so you must declare an event on the child component and listen to it with your parent component. I am guessing that there will be a control on the child component which causes the view to switch, so basically the child component requests to be hidden not shown.
EDIT: Since you want the child components to decide at run time which one of them is the one to display, you can't have them be hidden by *ngIf
because then if they are hidden they are not even instantiated, and they cannot run any code which causes them to reappear. In this case you need to have them all present, but hidden from view using CSS.
The event in the child component can be declared like this:
@Output() hideMe = new EventEmitter<any>();
And the listening at the parent component is done like this:
<app-child-1 [class.hidden]="!showList" (hideMe)="showList=false"></app-child-1>
<app-child-2 [class.hidden]="showList" (hideMe)="showList=true"></app-child-2>
And the accompanying CSS:
.hidden display: none;
Then in your child component logic, when it's time to emit the event you should do:
this.hideMe.emit();
edited Nov 10 at 8:40
answered Nov 9 at 16:33
Aviad P.
17.5k77295
17.5k77295
Yes, I've tried this option. If this.hideMe.emit(); is called during initialization it doesn't work. I mean in the ngOnInit method or in the ngAfterViewInit
– urDMG
Nov 9 at 17:31
You don't need to emit anything during initialization. You just need to supply a default value for the parent component variableshowList
.
– Aviad P.
Nov 9 at 17:41
Probably I didn't explain correctly. I need it during initialization. Child components are interchangeable. By the default (showList=true) in the parent component, child-component-1 is displayed. But I want to change showList property during initialization from the child-component-2 if condition is satisfied. I've tried to do in the child component 2 ngOnInit() { if(condition) this.hideMe.emit(); But it doesn't work
– urDMG
Nov 10 at 5:45
I think that because the hidden component is hidden with*ngIf
it is not even there to run its code, so it can't send any messages. I'll edit my answer.
– Aviad P.
Nov 10 at 8:37
All you have to do is to emit that output from child componant and then showhide everytime you emit the function
– Talg123
Nov 10 at 8:45
|
show 2 more comments
Yes, I've tried this option. If this.hideMe.emit(); is called during initialization it doesn't work. I mean in the ngOnInit method or in the ngAfterViewInit
– urDMG
Nov 9 at 17:31
You don't need to emit anything during initialization. You just need to supply a default value for the parent component variableshowList
.
– Aviad P.
Nov 9 at 17:41
Probably I didn't explain correctly. I need it during initialization. Child components are interchangeable. By the default (showList=true) in the parent component, child-component-1 is displayed. But I want to change showList property during initialization from the child-component-2 if condition is satisfied. I've tried to do in the child component 2 ngOnInit() { if(condition) this.hideMe.emit(); But it doesn't work
– urDMG
Nov 10 at 5:45
I think that because the hidden component is hidden with*ngIf
it is not even there to run its code, so it can't send any messages. I'll edit my answer.
– Aviad P.
Nov 10 at 8:37
All you have to do is to emit that output from child componant and then showhide everytime you emit the function
– Talg123
Nov 10 at 8:45
Yes, I've tried this option. If this.hideMe.emit(); is called during initialization it doesn't work. I mean in the ngOnInit method or in the ngAfterViewInit
– urDMG
Nov 9 at 17:31
Yes, I've tried this option. If this.hideMe.emit(); is called during initialization it doesn't work. I mean in the ngOnInit method or in the ngAfterViewInit
– urDMG
Nov 9 at 17:31
You don't need to emit anything during initialization. You just need to supply a default value for the parent component variable
showList
.– Aviad P.
Nov 9 at 17:41
You don't need to emit anything during initialization. You just need to supply a default value for the parent component variable
showList
.– Aviad P.
Nov 9 at 17:41
Probably I didn't explain correctly. I need it during initialization. Child components are interchangeable. By the default (showList=true) in the parent component, child-component-1 is displayed. But I want to change showList property during initialization from the child-component-2 if condition is satisfied. I've tried to do in the child component 2 ngOnInit() { if(condition) this.hideMe.emit(); But it doesn't work
– urDMG
Nov 10 at 5:45
Probably I didn't explain correctly. I need it during initialization. Child components are interchangeable. By the default (showList=true) in the parent component, child-component-1 is displayed. But I want to change showList property during initialization from the child-component-2 if condition is satisfied. I've tried to do in the child component 2 ngOnInit() { if(condition) this.hideMe.emit(); But it doesn't work
– urDMG
Nov 10 at 5:45
I think that because the hidden component is hidden with
*ngIf
it is not even there to run its code, so it can't send any messages. I'll edit my answer.– Aviad P.
Nov 10 at 8:37
I think that because the hidden component is hidden with
*ngIf
it is not even there to run its code, so it can't send any messages. I'll edit my answer.– Aviad P.
Nov 10 at 8:37
All you have to do is to emit that output from child componant and then showhide everytime you emit the function
– Talg123
Nov 10 at 8:45
All you have to do is to emit that output from child componant and then showhide everytime you emit the function
– Talg123
Nov 10 at 8:45
|
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229574%2fhow-child-component-could-control-visibility-of-itself-in-the-parent-on-child-co%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown