Service instantiated twice after APP_INITIALIZER











up vote
0
down vote

favorite












Problem is: I need to make an http call and store an object that is needed for generate dynamic routes. So, I was taking advantage of the APP_INITIALIZER.



// app.module.ts
import { ApplicationService } from './application.service';


providers: [
ApplicationService,
{
provide: APP_INITIALIZER, useFactory: appServiceFactory, deps:
[Injector, ApplicationService], multi: true
},
],

function appServiceFactory(injector: Injector, appService: ApplicationService): Function {
return () => {
return appService.loadApplication().then((app: Application) => {
/custom logic
});
});
};
}


// application.service.ts
@Injectable({
providedIn: 'root'
})

// navigation.component.ts
import { ApplicationService } from './application.service';

export class NavigationComponent implements OnInit {

constructor(private _applicationService: ApplicationService) {
}


}



But inside navigation.component, applicationservice is initialized again. I'm sure of that because if I log or put a debugger statement, the construct() method of the service is called twice.



Why even if the Service is declared as singleton with the providedIn: root is being reinstantiated?










share|improve this question
























  • Can you show use how you're importing module?
    – Pankaj Parkar
    Nov 9 at 13:04










  • @PankajParkar added
    – lucgenti
    Nov 9 at 13:15










  • I mean to say how you are importing module inside NgModule's imports array. this module is loaded lazily or eagerly?
    – Pankaj Parkar
    Nov 9 at 13:32












  • @PankajParkar the service is loaded inside the root module. I'm using only 1 module.
    – lucgenti
    Nov 9 at 13:34












  • what is appServiceFactory, I don't see that function anywhere in the code.
    – Pankaj Parkar
    Nov 9 at 13:39

















up vote
0
down vote

favorite












Problem is: I need to make an http call and store an object that is needed for generate dynamic routes. So, I was taking advantage of the APP_INITIALIZER.



// app.module.ts
import { ApplicationService } from './application.service';


providers: [
ApplicationService,
{
provide: APP_INITIALIZER, useFactory: appServiceFactory, deps:
[Injector, ApplicationService], multi: true
},
],

function appServiceFactory(injector: Injector, appService: ApplicationService): Function {
return () => {
return appService.loadApplication().then((app: Application) => {
/custom logic
});
});
};
}


// application.service.ts
@Injectable({
providedIn: 'root'
})

// navigation.component.ts
import { ApplicationService } from './application.service';

export class NavigationComponent implements OnInit {

constructor(private _applicationService: ApplicationService) {
}


}



But inside navigation.component, applicationservice is initialized again. I'm sure of that because if I log or put a debugger statement, the construct() method of the service is called twice.



Why even if the Service is declared as singleton with the providedIn: root is being reinstantiated?










share|improve this question
























  • Can you show use how you're importing module?
    – Pankaj Parkar
    Nov 9 at 13:04










  • @PankajParkar added
    – lucgenti
    Nov 9 at 13:15










  • I mean to say how you are importing module inside NgModule's imports array. this module is loaded lazily or eagerly?
    – Pankaj Parkar
    Nov 9 at 13:32












  • @PankajParkar the service is loaded inside the root module. I'm using only 1 module.
    – lucgenti
    Nov 9 at 13:34












  • what is appServiceFactory, I don't see that function anywhere in the code.
    – Pankaj Parkar
    Nov 9 at 13:39















up vote
0
down vote

favorite









up vote
0
down vote

favorite











Problem is: I need to make an http call and store an object that is needed for generate dynamic routes. So, I was taking advantage of the APP_INITIALIZER.



// app.module.ts
import { ApplicationService } from './application.service';


providers: [
ApplicationService,
{
provide: APP_INITIALIZER, useFactory: appServiceFactory, deps:
[Injector, ApplicationService], multi: true
},
],

function appServiceFactory(injector: Injector, appService: ApplicationService): Function {
return () => {
return appService.loadApplication().then((app: Application) => {
/custom logic
});
});
};
}


// application.service.ts
@Injectable({
providedIn: 'root'
})

// navigation.component.ts
import { ApplicationService } from './application.service';

export class NavigationComponent implements OnInit {

constructor(private _applicationService: ApplicationService) {
}


}



But inside navigation.component, applicationservice is initialized again. I'm sure of that because if I log or put a debugger statement, the construct() method of the service is called twice.



Why even if the Service is declared as singleton with the providedIn: root is being reinstantiated?










share|improve this question















Problem is: I need to make an http call and store an object that is needed for generate dynamic routes. So, I was taking advantage of the APP_INITIALIZER.



// app.module.ts
import { ApplicationService } from './application.service';


providers: [
ApplicationService,
{
provide: APP_INITIALIZER, useFactory: appServiceFactory, deps:
[Injector, ApplicationService], multi: true
},
],

function appServiceFactory(injector: Injector, appService: ApplicationService): Function {
return () => {
return appService.loadApplication().then((app: Application) => {
/custom logic
});
});
};
}


// application.service.ts
@Injectable({
providedIn: 'root'
})

// navigation.component.ts
import { ApplicationService } from './application.service';

export class NavigationComponent implements OnInit {

constructor(private _applicationService: ApplicationService) {
}


}



But inside navigation.component, applicationservice is initialized again. I'm sure of that because if I log or put a debugger statement, the construct() method of the service is called twice.



Why even if the Service is declared as singleton with the providedIn: root is being reinstantiated?







angular dependency-injection singleton angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 10:25

























asked Nov 9 at 12:56









lucgenti

13212




13212












  • Can you show use how you're importing module?
    – Pankaj Parkar
    Nov 9 at 13:04










  • @PankajParkar added
    – lucgenti
    Nov 9 at 13:15










  • I mean to say how you are importing module inside NgModule's imports array. this module is loaded lazily or eagerly?
    – Pankaj Parkar
    Nov 9 at 13:32












  • @PankajParkar the service is loaded inside the root module. I'm using only 1 module.
    – lucgenti
    Nov 9 at 13:34












  • what is appServiceFactory, I don't see that function anywhere in the code.
    – Pankaj Parkar
    Nov 9 at 13:39




















  • Can you show use how you're importing module?
    – Pankaj Parkar
    Nov 9 at 13:04










  • @PankajParkar added
    – lucgenti
    Nov 9 at 13:15










  • I mean to say how you are importing module inside NgModule's imports array. this module is loaded lazily or eagerly?
    – Pankaj Parkar
    Nov 9 at 13:32












  • @PankajParkar the service is loaded inside the root module. I'm using only 1 module.
    – lucgenti
    Nov 9 at 13:34












  • what is appServiceFactory, I don't see that function anywhere in the code.
    – Pankaj Parkar
    Nov 9 at 13:39


















Can you show use how you're importing module?
– Pankaj Parkar
Nov 9 at 13:04




Can you show use how you're importing module?
– Pankaj Parkar
Nov 9 at 13:04












@PankajParkar added
– lucgenti
Nov 9 at 13:15




@PankajParkar added
– lucgenti
Nov 9 at 13:15












I mean to say how you are importing module inside NgModule's imports array. this module is loaded lazily or eagerly?
– Pankaj Parkar
Nov 9 at 13:32






I mean to say how you are importing module inside NgModule's imports array. this module is loaded lazily or eagerly?
– Pankaj Parkar
Nov 9 at 13:32














@PankajParkar the service is loaded inside the root module. I'm using only 1 module.
– lucgenti
Nov 9 at 13:34






@PankajParkar the service is loaded inside the root module. I'm using only 1 module.
– lucgenti
Nov 9 at 13:34














what is appServiceFactory, I don't see that function anywhere in the code.
– Pankaj Parkar
Nov 9 at 13:39






what is appServiceFactory, I don't see that function anywhere in the code.
– Pankaj Parkar
Nov 9 at 13:39














3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted
+200










The reason for this is that once you include Router in dependencies to your APP_INITIALIZER factory you get circular dependency (https://github.com/angular/angular/blob/4c2ce4e8ba4c5ac5ce8754d67bc6603eaad4564a/packages/router/src/router_module.ts#L61-L64).



ApplicationService
|
TestService
|
Router
|
ApplicationRef
|
ApplicationInitStatus
|
APP_INITIALIZER
|
ApplicationService


To solve this you can get Router lazily:



export class TestService {

get router() {
return this.injector.get(Router)
}

constructor(private _http: HttpClient, private injector: Injector ) {
}
}


Forked Stackblitz






share|improve this answer




























    up vote
    1
    down vote













    Based on your Explanation that you have added the providedIn: root in the application.service.ts , that means it will be added to the root module (i.e Appmodule.ts) and again in the Appmodule.ts in your provider array you are adding ApplicationService.



    From this Blog its state that




    "There is now a new, recommended, way to register a provider, directly
    inside the @Injectable() decorator, using the new providedIn
    attribute. It accepts 'root' as a value or any module of your
    application. When you use 'root', your injectable will be registered
    as a singleton in the application, and you don’t need to add it to the
    providers of the root module. Similarly, if you use providedIn:
    UsersModule, the injectable is registered as a provider of the
    UsersModule without adding it to the providers of the module."




    this is creating the service to be reinstantiated



    Edit1 :
    Another thing to check is ,
    1. How are you calling this service i mean in Dev mode or Prod mode , if its dev mode then service will be called twice




    If you're running in development mode, it will run the function at
    least twice. since in development mode it does a check, changes, then
    rechecks to verify, where production mode only does the first check,
    assuming you've done your quality assurance and resolved any values
    the get changed post checking.




    Source



    Try to check in Prod Mode and verify it..






    share|improve this answer























    • If I remove the providedIn: root declaration, it still keeps being instantiated twice. Also, I can't remove it from the providers array because it's needed for the appServiceFactory function provided to APP_INITIALIZER
      – lucgenti
      Nov 12 at 13:23






    • 2




      @lucgenti it would be better if you can keep the code in slackblitz or any other means so that it will help in debugging more. right now we will be giving the solutions on case by case which is of no use
      – Webruster
      Nov 13 at 5:00










    • stackblitz.com/edit/angular-xwyugy but here seems like is getting instantiated just one time
      – lucgenti
      Nov 13 at 11:31










    • instead, in my app it keeps doing that even if enableProdMode();
      – lucgenti
      Nov 13 at 11:57










    • seems like the problem is not that the service is instantiated twice beacuse of dependency injection in a component. It's during the bootstrap that it's been called twice (1 for the module, 1 for the factory). So the data retrieved from the factory is lost.
      – lucgenti
      Nov 13 at 14:06




















    up vote
    0
    down vote













    Found the problem: it was because of the Router, needed as dependency by a service injected in ApplicationService.



    See example:
    https://stackblitz.com/edit/angular-xwyugy



    When Router is removed from the ApplicationService, the double instantiation is gone.



    Can't understand why, so I'll wait for a better answer for approving.






    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',
      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%2f53226116%2fservice-instantiated-twice-after-app-initializer%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted
      +200










      The reason for this is that once you include Router in dependencies to your APP_INITIALIZER factory you get circular dependency (https://github.com/angular/angular/blob/4c2ce4e8ba4c5ac5ce8754d67bc6603eaad4564a/packages/router/src/router_module.ts#L61-L64).



      ApplicationService
      |
      TestService
      |
      Router
      |
      ApplicationRef
      |
      ApplicationInitStatus
      |
      APP_INITIALIZER
      |
      ApplicationService


      To solve this you can get Router lazily:



      export class TestService {

      get router() {
      return this.injector.get(Router)
      }

      constructor(private _http: HttpClient, private injector: Injector ) {
      }
      }


      Forked Stackblitz






      share|improve this answer

























        up vote
        0
        down vote



        accepted
        +200










        The reason for this is that once you include Router in dependencies to your APP_INITIALIZER factory you get circular dependency (https://github.com/angular/angular/blob/4c2ce4e8ba4c5ac5ce8754d67bc6603eaad4564a/packages/router/src/router_module.ts#L61-L64).



        ApplicationService
        |
        TestService
        |
        Router
        |
        ApplicationRef
        |
        ApplicationInitStatus
        |
        APP_INITIALIZER
        |
        ApplicationService


        To solve this you can get Router lazily:



        export class TestService {

        get router() {
        return this.injector.get(Router)
        }

        constructor(private _http: HttpClient, private injector: Injector ) {
        }
        }


        Forked Stackblitz






        share|improve this answer























          up vote
          0
          down vote



          accepted
          +200







          up vote
          0
          down vote



          accepted
          +200




          +200




          The reason for this is that once you include Router in dependencies to your APP_INITIALIZER factory you get circular dependency (https://github.com/angular/angular/blob/4c2ce4e8ba4c5ac5ce8754d67bc6603eaad4564a/packages/router/src/router_module.ts#L61-L64).



          ApplicationService
          |
          TestService
          |
          Router
          |
          ApplicationRef
          |
          ApplicationInitStatus
          |
          APP_INITIALIZER
          |
          ApplicationService


          To solve this you can get Router lazily:



          export class TestService {

          get router() {
          return this.injector.get(Router)
          }

          constructor(private _http: HttpClient, private injector: Injector ) {
          }
          }


          Forked Stackblitz






          share|improve this answer












          The reason for this is that once you include Router in dependencies to your APP_INITIALIZER factory you get circular dependency (https://github.com/angular/angular/blob/4c2ce4e8ba4c5ac5ce8754d67bc6603eaad4564a/packages/router/src/router_module.ts#L61-L64).



          ApplicationService
          |
          TestService
          |
          Router
          |
          ApplicationRef
          |
          ApplicationInitStatus
          |
          APP_INITIALIZER
          |
          ApplicationService


          To solve this you can get Router lazily:



          export class TestService {

          get router() {
          return this.injector.get(Router)
          }

          constructor(private _http: HttpClient, private injector: Injector ) {
          }
          }


          Forked Stackblitz







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 at 18:40









          yurzui

          91k10176199




          91k10176199
























              up vote
              1
              down vote













              Based on your Explanation that you have added the providedIn: root in the application.service.ts , that means it will be added to the root module (i.e Appmodule.ts) and again in the Appmodule.ts in your provider array you are adding ApplicationService.



              From this Blog its state that




              "There is now a new, recommended, way to register a provider, directly
              inside the @Injectable() decorator, using the new providedIn
              attribute. It accepts 'root' as a value or any module of your
              application. When you use 'root', your injectable will be registered
              as a singleton in the application, and you don’t need to add it to the
              providers of the root module. Similarly, if you use providedIn:
              UsersModule, the injectable is registered as a provider of the
              UsersModule without adding it to the providers of the module."




              this is creating the service to be reinstantiated



              Edit1 :
              Another thing to check is ,
              1. How are you calling this service i mean in Dev mode or Prod mode , if its dev mode then service will be called twice




              If you're running in development mode, it will run the function at
              least twice. since in development mode it does a check, changes, then
              rechecks to verify, where production mode only does the first check,
              assuming you've done your quality assurance and resolved any values
              the get changed post checking.




              Source



              Try to check in Prod Mode and verify it..






              share|improve this answer























              • If I remove the providedIn: root declaration, it still keeps being instantiated twice. Also, I can't remove it from the providers array because it's needed for the appServiceFactory function provided to APP_INITIALIZER
                – lucgenti
                Nov 12 at 13:23






              • 2




                @lucgenti it would be better if you can keep the code in slackblitz or any other means so that it will help in debugging more. right now we will be giving the solutions on case by case which is of no use
                – Webruster
                Nov 13 at 5:00










              • stackblitz.com/edit/angular-xwyugy but here seems like is getting instantiated just one time
                – lucgenti
                Nov 13 at 11:31










              • instead, in my app it keeps doing that even if enableProdMode();
                – lucgenti
                Nov 13 at 11:57










              • seems like the problem is not that the service is instantiated twice beacuse of dependency injection in a component. It's during the bootstrap that it's been called twice (1 for the module, 1 for the factory). So the data retrieved from the factory is lost.
                – lucgenti
                Nov 13 at 14:06

















              up vote
              1
              down vote













              Based on your Explanation that you have added the providedIn: root in the application.service.ts , that means it will be added to the root module (i.e Appmodule.ts) and again in the Appmodule.ts in your provider array you are adding ApplicationService.



              From this Blog its state that




              "There is now a new, recommended, way to register a provider, directly
              inside the @Injectable() decorator, using the new providedIn
              attribute. It accepts 'root' as a value or any module of your
              application. When you use 'root', your injectable will be registered
              as a singleton in the application, and you don’t need to add it to the
              providers of the root module. Similarly, if you use providedIn:
              UsersModule, the injectable is registered as a provider of the
              UsersModule without adding it to the providers of the module."




              this is creating the service to be reinstantiated



              Edit1 :
              Another thing to check is ,
              1. How are you calling this service i mean in Dev mode or Prod mode , if its dev mode then service will be called twice




              If you're running in development mode, it will run the function at
              least twice. since in development mode it does a check, changes, then
              rechecks to verify, where production mode only does the first check,
              assuming you've done your quality assurance and resolved any values
              the get changed post checking.




              Source



              Try to check in Prod Mode and verify it..






              share|improve this answer























              • If I remove the providedIn: root declaration, it still keeps being instantiated twice. Also, I can't remove it from the providers array because it's needed for the appServiceFactory function provided to APP_INITIALIZER
                – lucgenti
                Nov 12 at 13:23






              • 2




                @lucgenti it would be better if you can keep the code in slackblitz or any other means so that it will help in debugging more. right now we will be giving the solutions on case by case which is of no use
                – Webruster
                Nov 13 at 5:00










              • stackblitz.com/edit/angular-xwyugy but here seems like is getting instantiated just one time
                – lucgenti
                Nov 13 at 11:31










              • instead, in my app it keeps doing that even if enableProdMode();
                – lucgenti
                Nov 13 at 11:57










              • seems like the problem is not that the service is instantiated twice beacuse of dependency injection in a component. It's during the bootstrap that it's been called twice (1 for the module, 1 for the factory). So the data retrieved from the factory is lost.
                – lucgenti
                Nov 13 at 14:06















              up vote
              1
              down vote










              up vote
              1
              down vote









              Based on your Explanation that you have added the providedIn: root in the application.service.ts , that means it will be added to the root module (i.e Appmodule.ts) and again in the Appmodule.ts in your provider array you are adding ApplicationService.



              From this Blog its state that




              "There is now a new, recommended, way to register a provider, directly
              inside the @Injectable() decorator, using the new providedIn
              attribute. It accepts 'root' as a value or any module of your
              application. When you use 'root', your injectable will be registered
              as a singleton in the application, and you don’t need to add it to the
              providers of the root module. Similarly, if you use providedIn:
              UsersModule, the injectable is registered as a provider of the
              UsersModule without adding it to the providers of the module."




              this is creating the service to be reinstantiated



              Edit1 :
              Another thing to check is ,
              1. How are you calling this service i mean in Dev mode or Prod mode , if its dev mode then service will be called twice




              If you're running in development mode, it will run the function at
              least twice. since in development mode it does a check, changes, then
              rechecks to verify, where production mode only does the first check,
              assuming you've done your quality assurance and resolved any values
              the get changed post checking.




              Source



              Try to check in Prod Mode and verify it..






              share|improve this answer














              Based on your Explanation that you have added the providedIn: root in the application.service.ts , that means it will be added to the root module (i.e Appmodule.ts) and again in the Appmodule.ts in your provider array you are adding ApplicationService.



              From this Blog its state that




              "There is now a new, recommended, way to register a provider, directly
              inside the @Injectable() decorator, using the new providedIn
              attribute. It accepts 'root' as a value or any module of your
              application. When you use 'root', your injectable will be registered
              as a singleton in the application, and you don’t need to add it to the
              providers of the root module. Similarly, if you use providedIn:
              UsersModule, the injectable is registered as a provider of the
              UsersModule without adding it to the providers of the module."




              this is creating the service to be reinstantiated



              Edit1 :
              Another thing to check is ,
              1. How are you calling this service i mean in Dev mode or Prod mode , if its dev mode then service will be called twice




              If you're running in development mode, it will run the function at
              least twice. since in development mode it does a check, changes, then
              rechecks to verify, where production mode only does the first check,
              assuming you've done your quality assurance and resolved any values
              the get changed post checking.




              Source



              Try to check in Prod Mode and verify it..







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 13 at 9:41

























              answered Nov 12 at 12:09









              Webruster

              7,26542138




              7,26542138












              • If I remove the providedIn: root declaration, it still keeps being instantiated twice. Also, I can't remove it from the providers array because it's needed for the appServiceFactory function provided to APP_INITIALIZER
                – lucgenti
                Nov 12 at 13:23






              • 2




                @lucgenti it would be better if you can keep the code in slackblitz or any other means so that it will help in debugging more. right now we will be giving the solutions on case by case which is of no use
                – Webruster
                Nov 13 at 5:00










              • stackblitz.com/edit/angular-xwyugy but here seems like is getting instantiated just one time
                – lucgenti
                Nov 13 at 11:31










              • instead, in my app it keeps doing that even if enableProdMode();
                – lucgenti
                Nov 13 at 11:57










              • seems like the problem is not that the service is instantiated twice beacuse of dependency injection in a component. It's during the bootstrap that it's been called twice (1 for the module, 1 for the factory). So the data retrieved from the factory is lost.
                – lucgenti
                Nov 13 at 14:06




















              • If I remove the providedIn: root declaration, it still keeps being instantiated twice. Also, I can't remove it from the providers array because it's needed for the appServiceFactory function provided to APP_INITIALIZER
                – lucgenti
                Nov 12 at 13:23






              • 2




                @lucgenti it would be better if you can keep the code in slackblitz or any other means so that it will help in debugging more. right now we will be giving the solutions on case by case which is of no use
                – Webruster
                Nov 13 at 5:00










              • stackblitz.com/edit/angular-xwyugy but here seems like is getting instantiated just one time
                – lucgenti
                Nov 13 at 11:31










              • instead, in my app it keeps doing that even if enableProdMode();
                – lucgenti
                Nov 13 at 11:57










              • seems like the problem is not that the service is instantiated twice beacuse of dependency injection in a component. It's during the bootstrap that it's been called twice (1 for the module, 1 for the factory). So the data retrieved from the factory is lost.
                – lucgenti
                Nov 13 at 14:06


















              If I remove the providedIn: root declaration, it still keeps being instantiated twice. Also, I can't remove it from the providers array because it's needed for the appServiceFactory function provided to APP_INITIALIZER
              – lucgenti
              Nov 12 at 13:23




              If I remove the providedIn: root declaration, it still keeps being instantiated twice. Also, I can't remove it from the providers array because it's needed for the appServiceFactory function provided to APP_INITIALIZER
              – lucgenti
              Nov 12 at 13:23




              2




              2




              @lucgenti it would be better if you can keep the code in slackblitz or any other means so that it will help in debugging more. right now we will be giving the solutions on case by case which is of no use
              – Webruster
              Nov 13 at 5:00




              @lucgenti it would be better if you can keep the code in slackblitz or any other means so that it will help in debugging more. right now we will be giving the solutions on case by case which is of no use
              – Webruster
              Nov 13 at 5:00












              stackblitz.com/edit/angular-xwyugy but here seems like is getting instantiated just one time
              – lucgenti
              Nov 13 at 11:31




              stackblitz.com/edit/angular-xwyugy but here seems like is getting instantiated just one time
              – lucgenti
              Nov 13 at 11:31












              instead, in my app it keeps doing that even if enableProdMode();
              – lucgenti
              Nov 13 at 11:57




              instead, in my app it keeps doing that even if enableProdMode();
              – lucgenti
              Nov 13 at 11:57












              seems like the problem is not that the service is instantiated twice beacuse of dependency injection in a component. It's during the bootstrap that it's been called twice (1 for the module, 1 for the factory). So the data retrieved from the factory is lost.
              – lucgenti
              Nov 13 at 14:06






              seems like the problem is not that the service is instantiated twice beacuse of dependency injection in a component. It's during the bootstrap that it's been called twice (1 for the module, 1 for the factory). So the data retrieved from the factory is lost.
              – lucgenti
              Nov 13 at 14:06












              up vote
              0
              down vote













              Found the problem: it was because of the Router, needed as dependency by a service injected in ApplicationService.



              See example:
              https://stackblitz.com/edit/angular-xwyugy



              When Router is removed from the ApplicationService, the double instantiation is gone.



              Can't understand why, so I'll wait for a better answer for approving.






              share|improve this answer

























                up vote
                0
                down vote













                Found the problem: it was because of the Router, needed as dependency by a service injected in ApplicationService.



                See example:
                https://stackblitz.com/edit/angular-xwyugy



                When Router is removed from the ApplicationService, the double instantiation is gone.



                Can't understand why, so I'll wait for a better answer for approving.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Found the problem: it was because of the Router, needed as dependency by a service injected in ApplicationService.



                  See example:
                  https://stackblitz.com/edit/angular-xwyugy



                  When Router is removed from the ApplicationService, the double instantiation is gone.



                  Can't understand why, so I'll wait for a better answer for approving.






                  share|improve this answer












                  Found the problem: it was because of the Router, needed as dependency by a service injected in ApplicationService.



                  See example:
                  https://stackblitz.com/edit/angular-xwyugy



                  When Router is removed from the ApplicationService, the double instantiation is gone.



                  Can't understand why, so I'll wait for a better answer for approving.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 at 8:41









                  lucgenti

                  13212




                  13212






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226116%2fservice-instantiated-twice-after-app-initializer%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

                      鏡平學校

                      ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                      Why https connections are so slow when debugging (stepping over) in Java?