Angular: Showing loading bar in a component when changing route@Directive v/s @Component in AngularHow to detect a route change in Angular?Angular - Use pipes in services and componentsAngular2 Navigation using Arrow KeysAngular/RxJs When should I unsubscribe from `Subscription`How to detect when an @Input() value changes in Angular?Angular2: how to set component to show after ajax loading of data?Loading components as soon as possibleAngular 4 LazyLoad child routes fails when child has same pathShow loading indicator in Angular while waiting for a RxJS observable

Have astronauts in space suits ever taken selfies? If so, how?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

What are the differences between the usage of 'it' and 'they'?

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Can I ask the recruiters in my resume to put the reason why I am rejected?

can i play a electric guitar through a bass amp?

Today is the Center

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

How can I make my BBEG immortal short of making them a Lich or Vampire?

Dragon forelimb placement

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

How much RAM could one put in a typical 80386 setup?

Python: next in for loop

What is the offset in a seaplane's hull?

Writing rule which states that two causes for the same superpower is bad writing

How is it possible to have an ability score that is less than 3?

Is it possible to rebuild the bike frame (to make it lighter) by welding aluminum tubes

How does one intimidate enemies without having the capacity for violence?

Why do falling prices hurt debtors?

Is it possible to do 50 km distance without any previous training?

A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?

What defenses are there against being summoned by the Gate spell?

Can I make popcorn with any corn?

How to say job offer in Mandarin/Cantonese?



Angular: Showing loading bar in a component when changing route


@Directive v/s @Component in AngularHow to detect a route change in Angular?Angular - Use pipes in services and componentsAngular2 Navigation using Arrow KeysAngular/RxJs When should I unsubscribe from `Subscription`How to detect when an @Input() value changes in Angular?Angular2: how to set component to show after ajax loading of data?Loading components as soon as possibleAngular 4 LazyLoad child routes fails when child has same pathShow loading indicator in Angular while waiting for a RxJS observable






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Not sure what is the best approach to achieve this.



I have a list of items at this URL: http://localhost/items



items list



When user clicks the edit button(let's say the item 2 button) it will change the route to http://localhost/items/item2



Now I want to get information from database for the selected item and show details information. While waiting for data from the server I want to show a loading progress where the items list is placed, while showing the menu and info components. Like this:



loading



One approach that I used to do this was to simply load the ItemDetails component , show the loading label, made a request to get details and when the data is received I hide the loading label.



 ngOnInit() 
this.loading = true;
const itemID = this.route.snapshot.paramMap.get('id');
this.itemsService
.getItem(itemID)
.subscribe(restult =>
//process result
this.loading = false;
);



This approach works great but I'm not sure if this is an anti-pattern or not.



The second approach I tried was using a resolver to get the item info and rely on router events(NavigationStart, NavigationEnd) to show the loading label.



The problem with this approach is when the user enters the item details URL in the browser( doesn't click the edit button) : http://localhost/items/item2. it will wait until the data is received then will render all components(menu,info and of course the item details).



So, it is OK to use the first approach or is it an anti pattern? If it's an anti pattern what's the best way to resolve this using a resolver or any other approach in a way that I'm able to show some components(menu,loading bar) while retrieving the data?










share|improve this question






















  • I think your approach is fine. Are you using SSR (Server-side rendering) or client-side rendering? Why im asking this, because if you control over your browser or page load event for first start, you can fire loading = true so control over your URL is the browser enters.

    – shadowman_93
    Mar 9 at 7:46

















0















Not sure what is the best approach to achieve this.



I have a list of items at this URL: http://localhost/items



items list



When user clicks the edit button(let's say the item 2 button) it will change the route to http://localhost/items/item2



Now I want to get information from database for the selected item and show details information. While waiting for data from the server I want to show a loading progress where the items list is placed, while showing the menu and info components. Like this:



loading



One approach that I used to do this was to simply load the ItemDetails component , show the loading label, made a request to get details and when the data is received I hide the loading label.



 ngOnInit() 
this.loading = true;
const itemID = this.route.snapshot.paramMap.get('id');
this.itemsService
.getItem(itemID)
.subscribe(restult =>
//process result
this.loading = false;
);



This approach works great but I'm not sure if this is an anti-pattern or not.



The second approach I tried was using a resolver to get the item info and rely on router events(NavigationStart, NavigationEnd) to show the loading label.



The problem with this approach is when the user enters the item details URL in the browser( doesn't click the edit button) : http://localhost/items/item2. it will wait until the data is received then will render all components(menu,info and of course the item details).



So, it is OK to use the first approach or is it an anti pattern? If it's an anti pattern what's the best way to resolve this using a resolver or any other approach in a way that I'm able to show some components(menu,loading bar) while retrieving the data?










share|improve this question






















  • I think your approach is fine. Are you using SSR (Server-side rendering) or client-side rendering? Why im asking this, because if you control over your browser or page load event for first start, you can fire loading = true so control over your URL is the browser enters.

    – shadowman_93
    Mar 9 at 7:46













0












0








0








Not sure what is the best approach to achieve this.



I have a list of items at this URL: http://localhost/items



items list



When user clicks the edit button(let's say the item 2 button) it will change the route to http://localhost/items/item2



Now I want to get information from database for the selected item and show details information. While waiting for data from the server I want to show a loading progress where the items list is placed, while showing the menu and info components. Like this:



loading



One approach that I used to do this was to simply load the ItemDetails component , show the loading label, made a request to get details and when the data is received I hide the loading label.



 ngOnInit() 
this.loading = true;
const itemID = this.route.snapshot.paramMap.get('id');
this.itemsService
.getItem(itemID)
.subscribe(restult =>
//process result
this.loading = false;
);



This approach works great but I'm not sure if this is an anti-pattern or not.



The second approach I tried was using a resolver to get the item info and rely on router events(NavigationStart, NavigationEnd) to show the loading label.



The problem with this approach is when the user enters the item details URL in the browser( doesn't click the edit button) : http://localhost/items/item2. it will wait until the data is received then will render all components(menu,info and of course the item details).



So, it is OK to use the first approach or is it an anti pattern? If it's an anti pattern what's the best way to resolve this using a resolver or any other approach in a way that I'm able to show some components(menu,loading bar) while retrieving the data?










share|improve this question














Not sure what is the best approach to achieve this.



I have a list of items at this URL: http://localhost/items



items list



When user clicks the edit button(let's say the item 2 button) it will change the route to http://localhost/items/item2



Now I want to get information from database for the selected item and show details information. While waiting for data from the server I want to show a loading progress where the items list is placed, while showing the menu and info components. Like this:



loading



One approach that I used to do this was to simply load the ItemDetails component , show the loading label, made a request to get details and when the data is received I hide the loading label.



 ngOnInit() 
this.loading = true;
const itemID = this.route.snapshot.paramMap.get('id');
this.itemsService
.getItem(itemID)
.subscribe(restult =>
//process result
this.loading = false;
);



This approach works great but I'm not sure if this is an anti-pattern or not.



The second approach I tried was using a resolver to get the item info and rely on router events(NavigationStart, NavigationEnd) to show the loading label.



The problem with this approach is when the user enters the item details URL in the browser( doesn't click the edit button) : http://localhost/items/item2. it will wait until the data is received then will render all components(menu,info and of course the item details).



So, it is OK to use the first approach or is it an anti pattern? If it's an anti pattern what's the best way to resolve this using a resolver or any other approach in a way that I'm able to show some components(menu,loading bar) while retrieving the data?







angular angular-router






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 9 at 2:56









Doua BeriDoua Beri

3,946125699




3,946125699












  • I think your approach is fine. Are you using SSR (Server-side rendering) or client-side rendering? Why im asking this, because if you control over your browser or page load event for first start, you can fire loading = true so control over your URL is the browser enters.

    – shadowman_93
    Mar 9 at 7:46

















  • I think your approach is fine. Are you using SSR (Server-side rendering) or client-side rendering? Why im asking this, because if you control over your browser or page load event for first start, you can fire loading = true so control over your URL is the browser enters.

    – shadowman_93
    Mar 9 at 7:46
















I think your approach is fine. Are you using SSR (Server-side rendering) or client-side rendering? Why im asking this, because if you control over your browser or page load event for first start, you can fire loading = true so control over your URL is the browser enters.

– shadowman_93
Mar 9 at 7:46





I think your approach is fine. Are you using SSR (Server-side rendering) or client-side rendering? Why im asking this, because if you control over your browser or page load event for first start, you can fire loading = true so control over your URL is the browser enters.

– shadowman_93
Mar 9 at 7:46












1 Answer
1






active

oldest

votes


















0














You can also use the npm package for the same, So you don't need to set loading flag for each request here is the npm package example.



Package: https://www.npmjs.com/package/ng-http-loader



$ npm install ng-http-loader --save / yarn add ng-http-loader



In AppModule:



`import BrowserModule from '@angular/platform-browser';
import NgModule from '@angular/core';
import AppComponent from './app.component';
import HttpClientModule from '@angular/common/http';
import NgHttpLoaderModule from 'ng-http-loader';

@NgModule(
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule, // <============ (Perform HTTP requests with this module)
NgHttpLoaderModule.forRoot(), // <============ Don't forget to call 'forRoot()'!
],
providers: [],
bootstrap: [AppComponent]
)
export class AppModule `


In app.component.html



<ng-http-loader></ng-http-loader>






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%2f55073574%2fangular-showing-loading-bar-in-a-component-when-changing-route%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









    0














    You can also use the npm package for the same, So you don't need to set loading flag for each request here is the npm package example.



    Package: https://www.npmjs.com/package/ng-http-loader



    $ npm install ng-http-loader --save / yarn add ng-http-loader



    In AppModule:



    `import BrowserModule from '@angular/platform-browser';
    import NgModule from '@angular/core';
    import AppComponent from './app.component';
    import HttpClientModule from '@angular/common/http';
    import NgHttpLoaderModule from 'ng-http-loader';

    @NgModule(
    declarations: [
    AppComponent
    ],
    imports: [
    BrowserModule,
    HttpClientModule, // <============ (Perform HTTP requests with this module)
    NgHttpLoaderModule.forRoot(), // <============ Don't forget to call 'forRoot()'!
    ],
    providers: [],
    bootstrap: [AppComponent]
    )
    export class AppModule `


    In app.component.html



    <ng-http-loader></ng-http-loader>






    share|improve this answer



























      0














      You can also use the npm package for the same, So you don't need to set loading flag for each request here is the npm package example.



      Package: https://www.npmjs.com/package/ng-http-loader



      $ npm install ng-http-loader --save / yarn add ng-http-loader



      In AppModule:



      `import BrowserModule from '@angular/platform-browser';
      import NgModule from '@angular/core';
      import AppComponent from './app.component';
      import HttpClientModule from '@angular/common/http';
      import NgHttpLoaderModule from 'ng-http-loader';

      @NgModule(
      declarations: [
      AppComponent
      ],
      imports: [
      BrowserModule,
      HttpClientModule, // <============ (Perform HTTP requests with this module)
      NgHttpLoaderModule.forRoot(), // <============ Don't forget to call 'forRoot()'!
      ],
      providers: [],
      bootstrap: [AppComponent]
      )
      export class AppModule `


      In app.component.html



      <ng-http-loader></ng-http-loader>






      share|improve this answer

























        0












        0








        0







        You can also use the npm package for the same, So you don't need to set loading flag for each request here is the npm package example.



        Package: https://www.npmjs.com/package/ng-http-loader



        $ npm install ng-http-loader --save / yarn add ng-http-loader



        In AppModule:



        `import BrowserModule from '@angular/platform-browser';
        import NgModule from '@angular/core';
        import AppComponent from './app.component';
        import HttpClientModule from '@angular/common/http';
        import NgHttpLoaderModule from 'ng-http-loader';

        @NgModule(
        declarations: [
        AppComponent
        ],
        imports: [
        BrowserModule,
        HttpClientModule, // <============ (Perform HTTP requests with this module)
        NgHttpLoaderModule.forRoot(), // <============ Don't forget to call 'forRoot()'!
        ],
        providers: [],
        bootstrap: [AppComponent]
        )
        export class AppModule `


        In app.component.html



        <ng-http-loader></ng-http-loader>






        share|improve this answer













        You can also use the npm package for the same, So you don't need to set loading flag for each request here is the npm package example.



        Package: https://www.npmjs.com/package/ng-http-loader



        $ npm install ng-http-loader --save / yarn add ng-http-loader



        In AppModule:



        `import BrowserModule from '@angular/platform-browser';
        import NgModule from '@angular/core';
        import AppComponent from './app.component';
        import HttpClientModule from '@angular/common/http';
        import NgHttpLoaderModule from 'ng-http-loader';

        @NgModule(
        declarations: [
        AppComponent
        ],
        imports: [
        BrowserModule,
        HttpClientModule, // <============ (Perform HTTP requests with this module)
        NgHttpLoaderModule.forRoot(), // <============ Don't forget to call 'forRoot()'!
        ],
        providers: [],
        bootstrap: [AppComponent]
        )
        export class AppModule `


        In app.component.html



        <ng-http-loader></ng-http-loader>







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 3:51









        Yash RamiYash Rami

        40137




        40137





























            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%2f55073574%2fangular-showing-loading-bar-in-a-component-when-changing-route%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

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived