Show element based on service variable (set by another component)












0















I've got an application where I'm trying to show or hide an element based on a public variable in a service. This variable is instantiated like show: boolean = false; but is then set from child components.



My problem is that show never seems to update its value.



In doing various debugs, i found that show is set to whatever it's initial value is, but once the view initialization finishes, my inspector shows that show is undefined, although this may not be related to my issue.



So how can I show a parent element from a child component?



Code below:



app.component.html



<div *ngIf="appService.show"> Loading </div>
<div *ngIf="!appService.show"> Content </div>


app.component



export class AppComponent implements OnInit {

constructor(private appService: AppService) {}

ngOnInit() {}
}


app.service



export class AppService {

public show: boolean = false;

showLoader() {
this.show = true;
}

hideLoader() {
this.show = false;
}
}


child.component



export class ChildComponent implements OnInit {

constructor(private appService: AppService) {}

// This is just an example, I've got the showLoader() method firing after a subscription completes.
ngOnInit(){
this.appService.showLoader();
}
}









share|improve this question





























    0















    I've got an application where I'm trying to show or hide an element based on a public variable in a service. This variable is instantiated like show: boolean = false; but is then set from child components.



    My problem is that show never seems to update its value.



    In doing various debugs, i found that show is set to whatever it's initial value is, but once the view initialization finishes, my inspector shows that show is undefined, although this may not be related to my issue.



    So how can I show a parent element from a child component?



    Code below:



    app.component.html



    <div *ngIf="appService.show"> Loading </div>
    <div *ngIf="!appService.show"> Content </div>


    app.component



    export class AppComponent implements OnInit {

    constructor(private appService: AppService) {}

    ngOnInit() {}
    }


    app.service



    export class AppService {

    public show: boolean = false;

    showLoader() {
    this.show = true;
    }

    hideLoader() {
    this.show = false;
    }
    }


    child.component



    export class ChildComponent implements OnInit {

    constructor(private appService: AppService) {}

    // This is just an example, I've got the showLoader() method firing after a subscription completes.
    ngOnInit(){
    this.appService.showLoader();
    }
    }









    share|improve this question



























      0












      0








      0


      0






      I've got an application where I'm trying to show or hide an element based on a public variable in a service. This variable is instantiated like show: boolean = false; but is then set from child components.



      My problem is that show never seems to update its value.



      In doing various debugs, i found that show is set to whatever it's initial value is, but once the view initialization finishes, my inspector shows that show is undefined, although this may not be related to my issue.



      So how can I show a parent element from a child component?



      Code below:



      app.component.html



      <div *ngIf="appService.show"> Loading </div>
      <div *ngIf="!appService.show"> Content </div>


      app.component



      export class AppComponent implements OnInit {

      constructor(private appService: AppService) {}

      ngOnInit() {}
      }


      app.service



      export class AppService {

      public show: boolean = false;

      showLoader() {
      this.show = true;
      }

      hideLoader() {
      this.show = false;
      }
      }


      child.component



      export class ChildComponent implements OnInit {

      constructor(private appService: AppService) {}

      // This is just an example, I've got the showLoader() method firing after a subscription completes.
      ngOnInit(){
      this.appService.showLoader();
      }
      }









      share|improve this question
















      I've got an application where I'm trying to show or hide an element based on a public variable in a service. This variable is instantiated like show: boolean = false; but is then set from child components.



      My problem is that show never seems to update its value.



      In doing various debugs, i found that show is set to whatever it's initial value is, but once the view initialization finishes, my inspector shows that show is undefined, although this may not be related to my issue.



      So how can I show a parent element from a child component?



      Code below:



      app.component.html



      <div *ngIf="appService.show"> Loading </div>
      <div *ngIf="!appService.show"> Content </div>


      app.component



      export class AppComponent implements OnInit {

      constructor(private appService: AppService) {}

      ngOnInit() {}
      }


      app.service



      export class AppService {

      public show: boolean = false;

      showLoader() {
      this.show = true;
      }

      hideLoader() {
      this.show = false;
      }
      }


      child.component



      export class ChildComponent implements OnInit {

      constructor(private appService: AppService) {}

      // This is just an example, I've got the showLoader() method firing after a subscription completes.
      ngOnInit(){
      this.appService.showLoader();
      }
      }






      angular angular7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 17:14







      expenguin

















      asked Jan 3 at 17:07









      expenguinexpenguin

      164114




      164114
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The best way to accomplish this is to create an observable to manage the loading state. I prefer to use a BehaviorSubject because it remembers the last value it emitted. You can have multiple components subscribe to the loading service. (more on behavior subjects here).



          Here is a link to a stackblitz where I have implemented a simple version of this loader service: https://stackblitz.com/edit/angular-loading-service-subscribe



          EDIT: I added the code for the child.component to the answer. I had it in the stackblitz, but forgot to add it here.



          Key notes:



          loading.service.ts has a BehaviorSubject that manages whether or not the app is loading data. It returns itself .asObservable() so no child components can call .next() on it.



          export class LoadingService { 

          private _isLoading$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);

          get isLoading$(): Observable<boolean> {
          return this._isLoading$.asObservable();
          }

          // see the rest of the code in the stackblitz
          }


          Once that is wired up, inject it into the component.



          app.component.ts



          @Component({
          selector: 'my-app',
          templateUrl: './app.component.html',
          styleUrls: ['./app.component.css']
          })
          export class AppComponent {
          // by making it public, you can subscribe in the template
          constructor(public loadingService: LoadingService) { }
          }


          app.component.html



          <ng-template [ngIf]="loadingService.isLoading$ | async" [ngIfElse]="loaded">
          Loading
          </ng-template>

          <ng-template #loaded>
          Content has loaded
          </ng-template>

          <!-- see child.component -->
          <child></child>


          the child.component can then control the loading indicator by using the loading.service.



          @Component({
          selector: 'child',
          template: `
          <h1>Child Component</h1>
          <button (click)="toggleLoading()">Change Loading</button>
          `,
          styles: [`h1 { font-family: Lato; }`]
          })
          export class ChildComponent {

          constructor(public loadingService: LoadingService) { }

          // simulates a 3 second load time for fetching data
          ngOnInit() {
          this.loadingService.startLoading();
          setTimeout(() => {
          this.loadingService.stopLoading();
          }, 3000);
          }

          // simple method to simulate user action that loads some new data
          toggleLoading(): void {
          this.loadingService.toggleLoading();
          }
          }


          check out this resource for info on the ng-template: https://toddmotto.com/angular-ngif-else-then



          and this one for the async pipe: https://angular.io/api/common/AsyncPipe






          share|improve this answer


























          • So I think this is the correct approach, however when I implemented your suggestions, I got the "Loading" template to show, but it never reaches the loaded state and constantly displays the "Loading" template. In my index.html view, I display a static title using interpolation so there should be some content displayed. Thinking on this, I should just be able to subscribe to the get isLoading$() method and then use the success output to determine that the loading is complete, correct?

            – expenguin
            Jan 3 at 18:51













          • I was actually able to answer my own question. I missed the code to actually turn it on/off going into my child components. Cheers!

            – expenguin
            Jan 3 at 19:16






          • 1





            Awesome! Glad you got it working. Sorry I didn't include all the code in my answer that is in my stackblitz code.

            – David House
            Jan 4 at 15:33



















          1














          Make the show field as observable.



          app.service



          import { Injectable } from '@angular/core';
          import { BehaviorSubject } from 'rxjs';

          @Injectable({
          providedIn: 'root'
          })
          export class AppService {

          public show$ = new BehaviorSubject(false);
          constructor() { }

          showLoader() {
          this.show$.next(true);
          }

          hideLoader() {
          this.show$.next(false);
          }
          }


          And subscribe wherever you need to listen it.






          share|improve this answer
























            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',
            autoActivateHeartbeat: false,
            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%2f54026762%2fshow-element-based-on-service-variable-set-by-another-component%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The best way to accomplish this is to create an observable to manage the loading state. I prefer to use a BehaviorSubject because it remembers the last value it emitted. You can have multiple components subscribe to the loading service. (more on behavior subjects here).



            Here is a link to a stackblitz where I have implemented a simple version of this loader service: https://stackblitz.com/edit/angular-loading-service-subscribe



            EDIT: I added the code for the child.component to the answer. I had it in the stackblitz, but forgot to add it here.



            Key notes:



            loading.service.ts has a BehaviorSubject that manages whether or not the app is loading data. It returns itself .asObservable() so no child components can call .next() on it.



            export class LoadingService { 

            private _isLoading$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);

            get isLoading$(): Observable<boolean> {
            return this._isLoading$.asObservable();
            }

            // see the rest of the code in the stackblitz
            }


            Once that is wired up, inject it into the component.



            app.component.ts



            @Component({
            selector: 'my-app',
            templateUrl: './app.component.html',
            styleUrls: ['./app.component.css']
            })
            export class AppComponent {
            // by making it public, you can subscribe in the template
            constructor(public loadingService: LoadingService) { }
            }


            app.component.html



            <ng-template [ngIf]="loadingService.isLoading$ | async" [ngIfElse]="loaded">
            Loading
            </ng-template>

            <ng-template #loaded>
            Content has loaded
            </ng-template>

            <!-- see child.component -->
            <child></child>


            the child.component can then control the loading indicator by using the loading.service.



            @Component({
            selector: 'child',
            template: `
            <h1>Child Component</h1>
            <button (click)="toggleLoading()">Change Loading</button>
            `,
            styles: [`h1 { font-family: Lato; }`]
            })
            export class ChildComponent {

            constructor(public loadingService: LoadingService) { }

            // simulates a 3 second load time for fetching data
            ngOnInit() {
            this.loadingService.startLoading();
            setTimeout(() => {
            this.loadingService.stopLoading();
            }, 3000);
            }

            // simple method to simulate user action that loads some new data
            toggleLoading(): void {
            this.loadingService.toggleLoading();
            }
            }


            check out this resource for info on the ng-template: https://toddmotto.com/angular-ngif-else-then



            and this one for the async pipe: https://angular.io/api/common/AsyncPipe






            share|improve this answer


























            • So I think this is the correct approach, however when I implemented your suggestions, I got the "Loading" template to show, but it never reaches the loaded state and constantly displays the "Loading" template. In my index.html view, I display a static title using interpolation so there should be some content displayed. Thinking on this, I should just be able to subscribe to the get isLoading$() method and then use the success output to determine that the loading is complete, correct?

              – expenguin
              Jan 3 at 18:51













            • I was actually able to answer my own question. I missed the code to actually turn it on/off going into my child components. Cheers!

              – expenguin
              Jan 3 at 19:16






            • 1





              Awesome! Glad you got it working. Sorry I didn't include all the code in my answer that is in my stackblitz code.

              – David House
              Jan 4 at 15:33
















            1














            The best way to accomplish this is to create an observable to manage the loading state. I prefer to use a BehaviorSubject because it remembers the last value it emitted. You can have multiple components subscribe to the loading service. (more on behavior subjects here).



            Here is a link to a stackblitz where I have implemented a simple version of this loader service: https://stackblitz.com/edit/angular-loading-service-subscribe



            EDIT: I added the code for the child.component to the answer. I had it in the stackblitz, but forgot to add it here.



            Key notes:



            loading.service.ts has a BehaviorSubject that manages whether or not the app is loading data. It returns itself .asObservable() so no child components can call .next() on it.



            export class LoadingService { 

            private _isLoading$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);

            get isLoading$(): Observable<boolean> {
            return this._isLoading$.asObservable();
            }

            // see the rest of the code in the stackblitz
            }


            Once that is wired up, inject it into the component.



            app.component.ts



            @Component({
            selector: 'my-app',
            templateUrl: './app.component.html',
            styleUrls: ['./app.component.css']
            })
            export class AppComponent {
            // by making it public, you can subscribe in the template
            constructor(public loadingService: LoadingService) { }
            }


            app.component.html



            <ng-template [ngIf]="loadingService.isLoading$ | async" [ngIfElse]="loaded">
            Loading
            </ng-template>

            <ng-template #loaded>
            Content has loaded
            </ng-template>

            <!-- see child.component -->
            <child></child>


            the child.component can then control the loading indicator by using the loading.service.



            @Component({
            selector: 'child',
            template: `
            <h1>Child Component</h1>
            <button (click)="toggleLoading()">Change Loading</button>
            `,
            styles: [`h1 { font-family: Lato; }`]
            })
            export class ChildComponent {

            constructor(public loadingService: LoadingService) { }

            // simulates a 3 second load time for fetching data
            ngOnInit() {
            this.loadingService.startLoading();
            setTimeout(() => {
            this.loadingService.stopLoading();
            }, 3000);
            }

            // simple method to simulate user action that loads some new data
            toggleLoading(): void {
            this.loadingService.toggleLoading();
            }
            }


            check out this resource for info on the ng-template: https://toddmotto.com/angular-ngif-else-then



            and this one for the async pipe: https://angular.io/api/common/AsyncPipe






            share|improve this answer


























            • So I think this is the correct approach, however when I implemented your suggestions, I got the "Loading" template to show, but it never reaches the loaded state and constantly displays the "Loading" template. In my index.html view, I display a static title using interpolation so there should be some content displayed. Thinking on this, I should just be able to subscribe to the get isLoading$() method and then use the success output to determine that the loading is complete, correct?

              – expenguin
              Jan 3 at 18:51













            • I was actually able to answer my own question. I missed the code to actually turn it on/off going into my child components. Cheers!

              – expenguin
              Jan 3 at 19:16






            • 1





              Awesome! Glad you got it working. Sorry I didn't include all the code in my answer that is in my stackblitz code.

              – David House
              Jan 4 at 15:33














            1












            1








            1







            The best way to accomplish this is to create an observable to manage the loading state. I prefer to use a BehaviorSubject because it remembers the last value it emitted. You can have multiple components subscribe to the loading service. (more on behavior subjects here).



            Here is a link to a stackblitz where I have implemented a simple version of this loader service: https://stackblitz.com/edit/angular-loading-service-subscribe



            EDIT: I added the code for the child.component to the answer. I had it in the stackblitz, but forgot to add it here.



            Key notes:



            loading.service.ts has a BehaviorSubject that manages whether or not the app is loading data. It returns itself .asObservable() so no child components can call .next() on it.



            export class LoadingService { 

            private _isLoading$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);

            get isLoading$(): Observable<boolean> {
            return this._isLoading$.asObservable();
            }

            // see the rest of the code in the stackblitz
            }


            Once that is wired up, inject it into the component.



            app.component.ts



            @Component({
            selector: 'my-app',
            templateUrl: './app.component.html',
            styleUrls: ['./app.component.css']
            })
            export class AppComponent {
            // by making it public, you can subscribe in the template
            constructor(public loadingService: LoadingService) { }
            }


            app.component.html



            <ng-template [ngIf]="loadingService.isLoading$ | async" [ngIfElse]="loaded">
            Loading
            </ng-template>

            <ng-template #loaded>
            Content has loaded
            </ng-template>

            <!-- see child.component -->
            <child></child>


            the child.component can then control the loading indicator by using the loading.service.



            @Component({
            selector: 'child',
            template: `
            <h1>Child Component</h1>
            <button (click)="toggleLoading()">Change Loading</button>
            `,
            styles: [`h1 { font-family: Lato; }`]
            })
            export class ChildComponent {

            constructor(public loadingService: LoadingService) { }

            // simulates a 3 second load time for fetching data
            ngOnInit() {
            this.loadingService.startLoading();
            setTimeout(() => {
            this.loadingService.stopLoading();
            }, 3000);
            }

            // simple method to simulate user action that loads some new data
            toggleLoading(): void {
            this.loadingService.toggleLoading();
            }
            }


            check out this resource for info on the ng-template: https://toddmotto.com/angular-ngif-else-then



            and this one for the async pipe: https://angular.io/api/common/AsyncPipe






            share|improve this answer















            The best way to accomplish this is to create an observable to manage the loading state. I prefer to use a BehaviorSubject because it remembers the last value it emitted. You can have multiple components subscribe to the loading service. (more on behavior subjects here).



            Here is a link to a stackblitz where I have implemented a simple version of this loader service: https://stackblitz.com/edit/angular-loading-service-subscribe



            EDIT: I added the code for the child.component to the answer. I had it in the stackblitz, but forgot to add it here.



            Key notes:



            loading.service.ts has a BehaviorSubject that manages whether or not the app is loading data. It returns itself .asObservable() so no child components can call .next() on it.



            export class LoadingService { 

            private _isLoading$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);

            get isLoading$(): Observable<boolean> {
            return this._isLoading$.asObservable();
            }

            // see the rest of the code in the stackblitz
            }


            Once that is wired up, inject it into the component.



            app.component.ts



            @Component({
            selector: 'my-app',
            templateUrl: './app.component.html',
            styleUrls: ['./app.component.css']
            })
            export class AppComponent {
            // by making it public, you can subscribe in the template
            constructor(public loadingService: LoadingService) { }
            }


            app.component.html



            <ng-template [ngIf]="loadingService.isLoading$ | async" [ngIfElse]="loaded">
            Loading
            </ng-template>

            <ng-template #loaded>
            Content has loaded
            </ng-template>

            <!-- see child.component -->
            <child></child>


            the child.component can then control the loading indicator by using the loading.service.



            @Component({
            selector: 'child',
            template: `
            <h1>Child Component</h1>
            <button (click)="toggleLoading()">Change Loading</button>
            `,
            styles: [`h1 { font-family: Lato; }`]
            })
            export class ChildComponent {

            constructor(public loadingService: LoadingService) { }

            // simulates a 3 second load time for fetching data
            ngOnInit() {
            this.loadingService.startLoading();
            setTimeout(() => {
            this.loadingService.stopLoading();
            }, 3000);
            }

            // simple method to simulate user action that loads some new data
            toggleLoading(): void {
            this.loadingService.toggleLoading();
            }
            }


            check out this resource for info on the ng-template: https://toddmotto.com/angular-ngif-else-then



            and this one for the async pipe: https://angular.io/api/common/AsyncPipe







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 4 at 15:41

























            answered Jan 3 at 18:16









            David HouseDavid House

            264




            264













            • So I think this is the correct approach, however when I implemented your suggestions, I got the "Loading" template to show, but it never reaches the loaded state and constantly displays the "Loading" template. In my index.html view, I display a static title using interpolation so there should be some content displayed. Thinking on this, I should just be able to subscribe to the get isLoading$() method and then use the success output to determine that the loading is complete, correct?

              – expenguin
              Jan 3 at 18:51













            • I was actually able to answer my own question. I missed the code to actually turn it on/off going into my child components. Cheers!

              – expenguin
              Jan 3 at 19:16






            • 1





              Awesome! Glad you got it working. Sorry I didn't include all the code in my answer that is in my stackblitz code.

              – David House
              Jan 4 at 15:33



















            • So I think this is the correct approach, however when I implemented your suggestions, I got the "Loading" template to show, but it never reaches the loaded state and constantly displays the "Loading" template. In my index.html view, I display a static title using interpolation so there should be some content displayed. Thinking on this, I should just be able to subscribe to the get isLoading$() method and then use the success output to determine that the loading is complete, correct?

              – expenguin
              Jan 3 at 18:51













            • I was actually able to answer my own question. I missed the code to actually turn it on/off going into my child components. Cheers!

              – expenguin
              Jan 3 at 19:16






            • 1





              Awesome! Glad you got it working. Sorry I didn't include all the code in my answer that is in my stackblitz code.

              – David House
              Jan 4 at 15:33

















            So I think this is the correct approach, however when I implemented your suggestions, I got the "Loading" template to show, but it never reaches the loaded state and constantly displays the "Loading" template. In my index.html view, I display a static title using interpolation so there should be some content displayed. Thinking on this, I should just be able to subscribe to the get isLoading$() method and then use the success output to determine that the loading is complete, correct?

            – expenguin
            Jan 3 at 18:51







            So I think this is the correct approach, however when I implemented your suggestions, I got the "Loading" template to show, but it never reaches the loaded state and constantly displays the "Loading" template. In my index.html view, I display a static title using interpolation so there should be some content displayed. Thinking on this, I should just be able to subscribe to the get isLoading$() method and then use the success output to determine that the loading is complete, correct?

            – expenguin
            Jan 3 at 18:51















            I was actually able to answer my own question. I missed the code to actually turn it on/off going into my child components. Cheers!

            – expenguin
            Jan 3 at 19:16





            I was actually able to answer my own question. I missed the code to actually turn it on/off going into my child components. Cheers!

            – expenguin
            Jan 3 at 19:16




            1




            1





            Awesome! Glad you got it working. Sorry I didn't include all the code in my answer that is in my stackblitz code.

            – David House
            Jan 4 at 15:33





            Awesome! Glad you got it working. Sorry I didn't include all the code in my answer that is in my stackblitz code.

            – David House
            Jan 4 at 15:33













            1














            Make the show field as observable.



            app.service



            import { Injectable } from '@angular/core';
            import { BehaviorSubject } from 'rxjs';

            @Injectable({
            providedIn: 'root'
            })
            export class AppService {

            public show$ = new BehaviorSubject(false);
            constructor() { }

            showLoader() {
            this.show$.next(true);
            }

            hideLoader() {
            this.show$.next(false);
            }
            }


            And subscribe wherever you need to listen it.






            share|improve this answer




























              1














              Make the show field as observable.



              app.service



              import { Injectable } from '@angular/core';
              import { BehaviorSubject } from 'rxjs';

              @Injectable({
              providedIn: 'root'
              })
              export class AppService {

              public show$ = new BehaviorSubject(false);
              constructor() { }

              showLoader() {
              this.show$.next(true);
              }

              hideLoader() {
              this.show$.next(false);
              }
              }


              And subscribe wherever you need to listen it.






              share|improve this answer


























                1












                1








                1







                Make the show field as observable.



                app.service



                import { Injectable } from '@angular/core';
                import { BehaviorSubject } from 'rxjs';

                @Injectable({
                providedIn: 'root'
                })
                export class AppService {

                public show$ = new BehaviorSubject(false);
                constructor() { }

                showLoader() {
                this.show$.next(true);
                }

                hideLoader() {
                this.show$.next(false);
                }
                }


                And subscribe wherever you need to listen it.






                share|improve this answer













                Make the show field as observable.



                app.service



                import { Injectable } from '@angular/core';
                import { BehaviorSubject } from 'rxjs';

                @Injectable({
                providedIn: 'root'
                })
                export class AppService {

                public show$ = new BehaviorSubject(false);
                constructor() { }

                showLoader() {
                this.show$.next(true);
                }

                hideLoader() {
                this.show$.next(false);
                }
                }


                And subscribe wherever you need to listen it.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 17:55









                Roman LitvinovRoman Litvinov

                1819




                1819






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026762%2fshow-element-based-on-service-variable-set-by-another-component%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

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas