Angular 2: Can't resolve all parameters for NotificationsService: (?, ?).

4,706 views
Skip to first unread message

HisDivineShadow

unread,
Jul 5, 2016, 10:04:08 AM7/5/16
to AngularJS
I get this error loading my app.  I'm injecting the NotificationsService into my app component.  The NotificationsService injects the FindService which also has a constructor that injects the AuthService.  

import { AuthService } from '../authservice';
import {FindService} from '../findservice/find-service'
 
export class NotificationsService {
   
constructor(private authSvc: AuthService, private findSvc: FindService) { }
 
    getNotifications
() {
       
if (this.authSvc.isLoggedin) {
           
//do some stuff to get randomList
           
return randomList;
       
}
       
return [];
   
}
}



import { Injectable } from '@angular/core';
import { AuthService } from '../authservice';
 
 
@Injectable()
export class FindPropertyService {
 
   
constructor(private authSvc: AuthService) {}
}





Lucas Lacroix

unread,
Jul 5, 2016, 10:06:19 AM7/5/16
to ang...@googlegroups.com
I do not see your component with it's list of providers nor do I see your bootstrap call with the list of providers.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Lucas Lacroix
Computer Scientist
System Technology Division, MEDITECH

HisDivineShadow

unread,
Jul 5, 2016, 10:39:54 AM7/5/16
to AngularJS
So, after some further reading I found that the parent component (i.e. the app component in this case) needs to have a provider for the injected service specified in any classes that are used within the component (even if they are services that are use indirectly like mine is).  So, I added the provider for the findService to my app component's provider list and it worked.  I think ng2's DI is strange in that my component is reliant on the parent component to know what service need to be injected in my class.  The find service is using hte AuthService which is specified in my bootstrap.  I would have expected that the AuthService would be injected as a result of it being specified in bootstrap.

BOOTSTRAP

import {APP_ROUTER_PROVIDERS} from "./app.routes";

bootstrap(App, [APP_ROUTER_PROVIDERS, disableDeprecaedForms(), provideForms()])
    .catch((err: any) => console.error(err));



app.routes

import { provideRouter, RouterConfig } from '@angular/router';
import { AuthService } from './authservice';
import { AuthGuard } from './authorize';

export const routes: RouterConfig = [
   
//some routes
];

export const APP_ROUTER_PROVIDERS = [
   
provideRouter(routes),
   
AuthGuard,
   
AuthService
];







On Tuesday, July 5, 2016 at 9:06:19 AM UTC-5, Lucas Lacroix wrote:
I do not see your component with it's list of providers nor do I see your bootstrap call with the list of providers.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Lucas Lacroix

unread,
Jul 5, 2016, 10:47:30 AM7/5/16
to ang...@googlegroups.com
I think you're missing the FindService in your providers that are passed to bootstrap. I do not see it referenced.

As long as a provider is specified for each injectable, this should work.
Reply all
Reply to author
Forward
0 new messages