Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new instance of services #51

Closed
miton18 opened this issue Feb 13, 2017 · 2 comments
Closed

new instance of services #51

miton18 opened this issue Feb 13, 2017 · 2 comments

Comments

@miton18
Copy link

miton18 commented Feb 13, 2017

Hi ! some problems with my States.

In my @NgModule > providers, I add a service named "myService", beacause I want a unique instance of my service in the whole App. In my components I can inject my services which share the same instance of the service.
So now, in my State declaration, I add a resolvable :

{
      name: 'myState',
      url: '/user/:userId',
      component: ViewUserComponent,
      resolve: [
        new Resolvable('user', (http, trans, uService) => {
          return new Promise((resolve, reject) => {
            let id = trans.params().userId;
            http
            .get('myUrl ' + id)
            .subscribe((res: Response) => {
              let u = new User(res.json());
              new uService.add(u);
              resolve(u);
            }, reject);
          });
        }, [Http, Transition, userService])
      ]
    }

In my userService constructor I put a console.log, and I can see multiple "construct UserService". Xhy UIRouter don't take the app instance of the service? How can I force him to use this one ?

@miton18
Copy link
Author

miton18 commented Feb 14, 2017

the only solution I have while it's not fixed :

@NgModule({
  declarations: [
    AppComponent,
    _UIROUTER_DIRECTIVES
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [
    myService,
    _UIROUTER_INSTANCE_PROVIDERS,
    _UIROUTER_SERVICE_PROVIDERS,
    locationStrategy(routerConfig.useHash)
  ].concat(makeRootProviders(routerConfig)),
  bootstrap: [AppComponent]
})
export class AppModule { }

@christopherthielen
Copy link
Member

@miton18 is this still an issue for you?

The new uService.add(u); line looks suspicious. UI-Router will not itself create new instances of global services for injection. It either fetches an object from the resolve system or from Angular's DI system. Here's a stackblitz showing how it should work: https://stackblitz.com/edit/ui-router-angular-wgqmfj?file=app%2Fapp.module.ts

Let me know if this is still an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants