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.










share|improve this question

























    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.










    share|improve this question























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 16:23









      urDMG

      11318




      11318






















          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();





          share|improve this answer






















          • 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










          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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

























          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();





          share|improve this answer






















          • 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










          • 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














          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();





          share|improve this answer






















          • 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










          • 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












          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();





          share|improve this answer














          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();






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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











          • 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











          • 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











          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo