Retrieving and reacting to route params in FactoryProvider












0















I want to use a FactoryProvider that gets chooses a service implementation based on current URI.



Furthermore, one of the implementations needs to react to Route Parameters (as defined by route/:parameter).



As shown below, I subscribe to changes of Unfortunately, I can't access the parameters that way. A component that is instantiated in the primary router outlet can access them, but the FactoryProvider can't.



Is there anything I might have missed?



module code (abbreviated for improved readability)



@NgModule({
declarations: ,
imports: ,
exports: ,
providers: [XServiceProvider],
entryComponents: })


Factory Provider



export const XServiceProvider = {
provide: XService,
useFactory: XServiceFactory,
deps: [Http, HttpService, ConfigurationService, DatePipe, Router, ActivatedRoute]
};

const XServiceFactory = (http: Http,
httpService: HttpService,
configuration: ConfigurationService,
datePipe: DatePipe,
router: Router,
activatedRoute: ActivatedRoute) => {

const useAlternateImpplementation= router.url.startsWith('/mg/');


if (useAlternateImpplementation === true) {
const service = new XyService(http, httpService, configuration, datePipe);

for (const child of activatedRoute.root.children) {
if (child.outlet === PRIMARY_OUTLET) {
child.url.subscribe(params => {
service.setParam(params[parameter]);
});
}
}
return service;
}
return new XService(http, httpService, configuration, datePipe);
};









share|improve this question





























    0















    I want to use a FactoryProvider that gets chooses a service implementation based on current URI.



    Furthermore, one of the implementations needs to react to Route Parameters (as defined by route/:parameter).



    As shown below, I subscribe to changes of Unfortunately, I can't access the parameters that way. A component that is instantiated in the primary router outlet can access them, but the FactoryProvider can't.



    Is there anything I might have missed?



    module code (abbreviated for improved readability)



    @NgModule({
    declarations: ,
    imports: ,
    exports: ,
    providers: [XServiceProvider],
    entryComponents: })


    Factory Provider



    export const XServiceProvider = {
    provide: XService,
    useFactory: XServiceFactory,
    deps: [Http, HttpService, ConfigurationService, DatePipe, Router, ActivatedRoute]
    };

    const XServiceFactory = (http: Http,
    httpService: HttpService,
    configuration: ConfigurationService,
    datePipe: DatePipe,
    router: Router,
    activatedRoute: ActivatedRoute) => {

    const useAlternateImpplementation= router.url.startsWith('/mg/');


    if (useAlternateImpplementation === true) {
    const service = new XyService(http, httpService, configuration, datePipe);

    for (const child of activatedRoute.root.children) {
    if (child.outlet === PRIMARY_OUTLET) {
    child.url.subscribe(params => {
    service.setParam(params[parameter]);
    });
    }
    }
    return service;
    }
    return new XService(http, httpService, configuration, datePipe);
    };









    share|improve this question



























      0












      0








      0








      I want to use a FactoryProvider that gets chooses a service implementation based on current URI.



      Furthermore, one of the implementations needs to react to Route Parameters (as defined by route/:parameter).



      As shown below, I subscribe to changes of Unfortunately, I can't access the parameters that way. A component that is instantiated in the primary router outlet can access them, but the FactoryProvider can't.



      Is there anything I might have missed?



      module code (abbreviated for improved readability)



      @NgModule({
      declarations: ,
      imports: ,
      exports: ,
      providers: [XServiceProvider],
      entryComponents: })


      Factory Provider



      export const XServiceProvider = {
      provide: XService,
      useFactory: XServiceFactory,
      deps: [Http, HttpService, ConfigurationService, DatePipe, Router, ActivatedRoute]
      };

      const XServiceFactory = (http: Http,
      httpService: HttpService,
      configuration: ConfigurationService,
      datePipe: DatePipe,
      router: Router,
      activatedRoute: ActivatedRoute) => {

      const useAlternateImpplementation= router.url.startsWith('/mg/');


      if (useAlternateImpplementation === true) {
      const service = new XyService(http, httpService, configuration, datePipe);

      for (const child of activatedRoute.root.children) {
      if (child.outlet === PRIMARY_OUTLET) {
      child.url.subscribe(params => {
      service.setParam(params[parameter]);
      });
      }
      }
      return service;
      }
      return new XService(http, httpService, configuration, datePipe);
      };









      share|improve this question
















      I want to use a FactoryProvider that gets chooses a service implementation based on current URI.



      Furthermore, one of the implementations needs to react to Route Parameters (as defined by route/:parameter).



      As shown below, I subscribe to changes of Unfortunately, I can't access the parameters that way. A component that is instantiated in the primary router outlet can access them, but the FactoryProvider can't.



      Is there anything I might have missed?



      module code (abbreviated for improved readability)



      @NgModule({
      declarations: ,
      imports: ,
      exports: ,
      providers: [XServiceProvider],
      entryComponents: })


      Factory Provider



      export const XServiceProvider = {
      provide: XService,
      useFactory: XServiceFactory,
      deps: [Http, HttpService, ConfigurationService, DatePipe, Router, ActivatedRoute]
      };

      const XServiceFactory = (http: Http,
      httpService: HttpService,
      configuration: ConfigurationService,
      datePipe: DatePipe,
      router: Router,
      activatedRoute: ActivatedRoute) => {

      const useAlternateImpplementation= router.url.startsWith('/mg/');


      if (useAlternateImpplementation === true) {
      const service = new XyService(http, httpService, configuration, datePipe);

      for (const child of activatedRoute.root.children) {
      if (child.outlet === PRIMARY_OUTLET) {
      child.url.subscribe(params => {
      service.setParam(params[parameter]);
      });
      }
      }
      return service;
      }
      return new XService(http, httpService, configuration, datePipe);
      };






      angular angular-routing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 8:24









      Yashwardhan Pauranik

      2,03111528




      2,03111528










      asked Nov 27 '18 at 14:35









      Sebastian EdelmeierSebastian Edelmeier

      2,80222852




      2,80222852
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Solved the issue by changing the dependency injection scope from Singleton to Transient.



          Here's how:




          • remove the provider definition from the module

          • add it to the components that actually require it


          Code sample:



          @NgModule({
          declarations: ,
          exports: ,
          providers: [ /* remove here! */ ]
          })
          //...

          @Component({
          selector: '...',
          templateUrl: '...',
          styleUrls: ['...'],
          providers: [XServiceProvider] /* add here */
          })
          //...


          Here's (probably) why:




          • Services that are declared as module-wide providers are created once per application creation. Thus, you cannot load the app, use service implementation one, change context, use implementation two.

          • Declaring the providers per component changes this pattern. The components are instantiated and destroyed, based on routing. That said, another route can now lead to another implementation.


          I was inspired by a blog post to try this.



          Still, I have no idea how to access route params beyond the scope of components that a rendered to the primary outlet.






          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%2f53502033%2fretrieving-and-reacting-to-route-params-in-factoryprovider%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














            Solved the issue by changing the dependency injection scope from Singleton to Transient.



            Here's how:




            • remove the provider definition from the module

            • add it to the components that actually require it


            Code sample:



            @NgModule({
            declarations: ,
            exports: ,
            providers: [ /* remove here! */ ]
            })
            //...

            @Component({
            selector: '...',
            templateUrl: '...',
            styleUrls: ['...'],
            providers: [XServiceProvider] /* add here */
            })
            //...


            Here's (probably) why:




            • Services that are declared as module-wide providers are created once per application creation. Thus, you cannot load the app, use service implementation one, change context, use implementation two.

            • Declaring the providers per component changes this pattern. The components are instantiated and destroyed, based on routing. That said, another route can now lead to another implementation.


            I was inspired by a blog post to try this.



            Still, I have no idea how to access route params beyond the scope of components that a rendered to the primary outlet.






            share|improve this answer






























              0














              Solved the issue by changing the dependency injection scope from Singleton to Transient.



              Here's how:




              • remove the provider definition from the module

              • add it to the components that actually require it


              Code sample:



              @NgModule({
              declarations: ,
              exports: ,
              providers: [ /* remove here! */ ]
              })
              //...

              @Component({
              selector: '...',
              templateUrl: '...',
              styleUrls: ['...'],
              providers: [XServiceProvider] /* add here */
              })
              //...


              Here's (probably) why:




              • Services that are declared as module-wide providers are created once per application creation. Thus, you cannot load the app, use service implementation one, change context, use implementation two.

              • Declaring the providers per component changes this pattern. The components are instantiated and destroyed, based on routing. That said, another route can now lead to another implementation.


              I was inspired by a blog post to try this.



              Still, I have no idea how to access route params beyond the scope of components that a rendered to the primary outlet.






              share|improve this answer




























                0












                0








                0







                Solved the issue by changing the dependency injection scope from Singleton to Transient.



                Here's how:




                • remove the provider definition from the module

                • add it to the components that actually require it


                Code sample:



                @NgModule({
                declarations: ,
                exports: ,
                providers: [ /* remove here! */ ]
                })
                //...

                @Component({
                selector: '...',
                templateUrl: '...',
                styleUrls: ['...'],
                providers: [XServiceProvider] /* add here */
                })
                //...


                Here's (probably) why:




                • Services that are declared as module-wide providers are created once per application creation. Thus, you cannot load the app, use service implementation one, change context, use implementation two.

                • Declaring the providers per component changes this pattern. The components are instantiated and destroyed, based on routing. That said, another route can now lead to another implementation.


                I was inspired by a blog post to try this.



                Still, I have no idea how to access route params beyond the scope of components that a rendered to the primary outlet.






                share|improve this answer















                Solved the issue by changing the dependency injection scope from Singleton to Transient.



                Here's how:




                • remove the provider definition from the module

                • add it to the components that actually require it


                Code sample:



                @NgModule({
                declarations: ,
                exports: ,
                providers: [ /* remove here! */ ]
                })
                //...

                @Component({
                selector: '...',
                templateUrl: '...',
                styleUrls: ['...'],
                providers: [XServiceProvider] /* add here */
                })
                //...


                Here's (probably) why:




                • Services that are declared as module-wide providers are created once per application creation. Thus, you cannot load the app, use service implementation one, change context, use implementation two.

                • Declaring the providers per component changes this pattern. The components are instantiated and destroyed, based on routing. That said, another route can now lead to another implementation.


                I was inspired by a blog post to try this.



                Still, I have no idea how to access route params beyond the scope of components that a rendered to the primary outlet.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 3 '18 at 12:36

























                answered Dec 3 '18 at 11:00









                Sebastian EdelmeierSebastian Edelmeier

                2,80222852




                2,80222852
































                    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%2f53502033%2fretrieving-and-reacting-to-route-params-in-factoryprovider%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

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)