{"action":"created","issue":{"url":"https://api.github.com/repos/angular/angular/issues/10808","repository_url":"https://api.github.com/repos/angular/angular","labels_url":"https://api.github.com/repos/angular/angular/issues/10808/labels{/name}","comments_url":"https://api.github.com/repos/angular/angular/issues/10808/comments","events_url":"https://api.github.com/repos/angular/angular/issues/10808/events","html_url":"https://github.com/angular/angular/issues/10808","id":171140020,"number":10808,"title":"Dependency injection in nested modules doesn't seem to work","user":{"login":"tom-mayer","id":2562379,"avatar_url":"https://avatars.githubusercontent.com/u/2562379?v=3","gravatar_id":"","url":"https://api.github.com/users/tom-mayer","html_url":"https://github.com/tom-mayer","followers_url":"https://api.github.com/users/tom-mayer/followers","following_url":"https://api.github.com/users/tom-mayer/following{/other_user}","gists_url":"https://api.github.com/users/tom-mayer/gists{/gist_id}","starred_url":"https://api.github.com/users/tom-mayer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tom-mayer/subscriptions","organizations_url":"https://api.github.com/users/tom-mayer/orgs","repos_url":"https://api.github.com/users/tom-mayer/repos","events_url":"https://api.github.com/users/tom-mayer/events{/privacy}","received_events_url":"https://api.github.com/users/tom-mayer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-08-15T09:47:11Z","updated_at":"2016-08-15T14:57:02Z","closed_at":null,"body":"**I'm submitting a ...**  (check one with \"x\")\r\n```\r\n[x ] bug report => search github for a similar issue or PR before submitting\r\n[ ] feature request\r\n[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question\r\n```\r\n\r\n**Current behavior**\r\nI have an app module and modules for my sub components. In the ngDoBootstrap() of the main app module I initialize some shared services and give the created instance to the different sub components like so: \r\n\r\n```\r\nngDoBootstrap(){\r\n  var sharedService = new MyService();\r\n  var provider = {provide: MyService, useValue: sharedService};\r\n  ...\r\n  platformBrowserDynamic().bootstrapModule(MyOtherModule, {providers: providers);\r\n ...\r\n}\r\n```\r\nMyOtherModule is a simple wrapper for a component that just uses 'declarations' and 'bootstrap' to bootstrap a component. The component now crashes and says that there is no provider for MyService.\r\n\r\n**Expected/desired behavior**\r\nSince the component has no providers array it should look for a provider for MyService in the Module from which it was bootstrapped. Since the module has explicitly been given a provider for MyService, the component should get the instance sharedService when asking for a MyService.\r\n\r\n**Reproduction of the problem**\r\nA more verbose example is also listed here on stack:\r\nhttp://stackoverflow.com/questions/38896842/migrating-conditional-boostrap-of-multiple-angular-apps-with-shared-services-to\r\n\r\n\r\n\r\n**Please tell us about your environment:** MacOS El Capitan\r\n\r\n* **Angular version:** 2.0.0-rc.5\r\n \r\n* **Browser:** [Chrome 52] \r\n \r\n* **Language:** [TypeScript tsc version 1.8.10] \r\n"},"comment":{"url":"https://api.github.com/repos/angular/angular/issues/comments/239823985","html_url":"https://github.com/angular/angular/issues/10808#issuecomment-239823985","issue_url":"https://api.github.com/repos/angular/angular/issues/10808","id":239823985,"user":{"login":"gdi2290","id":1016365,"avatar_url":"https://avatars.githubusercontent.com/u/1016365?v=3","gravatar_id":"","url":"https://api.github.com/users/gdi2290","html_url":"https://github.com/gdi2290","followers_url":"https://api.github.com/users/gdi2290/followers","following_url":"https://api.github.com/users/gdi2290/following{/other_user}","gists_url":"https://api.github.com/users/gdi2290/gists{/gist_id}","starred_url":"https://api.github.com/users/gdi2290/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gdi2290/subscriptions","organizations_url":"https://api.github.com/users/gdi2290/orgs","repos_url":"https://api.github.com/users/gdi2290/repos","events_url":"https://api.github.com/users/gdi2290/events{/privacy}","received_events_url":"https://api.github.com/users/gdi2290/received_events","type":"User","site_admin":false},"created_at":"2016-08-15T14:57:02Z","updated_at":"2016-08-15T14:57:02Z","body":"the `ngDoBootstrap` lifecycle hook doesn't work like that. what you're expecting is something like `ngOnBootstrap` or `ngAfterBootstrap` or `ngOnInit` none of them work on NgModule though. For `ngDoBootstrap` what you're looking to do is \r\n```\r\nngDoBootstrap(moduleRef) {\r\n  const appRef = moduleRef.injector.get(ApplicationRef);\r\n  // somehow grab compFactory probably from entryComponents\r\n  // bootstrap each entryCoponent\r\n  appRef.bootstrap(compFactory);\r\n}\r\n```\r\nbut there's a problem with the `ApplicationRef#bootstrap` since it doesn't take an injector or providers anymore. You're better off doing this outside of NgModule and create an NgModule for each app. Or use Di in ngDoBootstrap to grab your service that way it's created during bootstrap rather than later\r\n```\r\nmoduleRef.injector.get(MyService); // this makes sure that the service is created during bootstrap rather than later when something else injects it\r\n```"}}
{"actor":{"display_login":"gdi2290"}}