I've been struggling around injecting services
into each other. The following blog Circular Dependency in constructors and Dependency Injection is kind of confusing where it says
One of the two objects is hiding another object C
I get the following error while injecting Service class into each other
Can't resolve all parameters for PayrollService: (SiteService, StorageService, SweetAlertService, ?)
//abstractmodal.service.ts
@Injectable()
export abstract class AbstractModel {
abstract collection = [];
constructor(private siteService: SiteService, private storageService: StorageService,
private sweetalertService: SweetAlertService) {}
setCollectionEmpty() {
this.collection = [];
}
}
//account-payable.service.ts
@Injectable()
export class AccountPayableService extends AbstractModel {
public collection = [];
constructor(private sS: SiteService,private stS: StorageService, private sws: SweetAlertService,
private accpPoService: PayablePurchaseOrderService, private attachmentService: AttachmentService,
private injectorService: InjectorService) {
super(sS, stS, sws);
}
}
//injector.service.ts
@Injectable()
export class InjectorService {
constructor(private payrollService: PayrollService) {}
cleanPayrollCollection() {
this.payrollService.setCollectionEmpty();
}
}
//payroll.service.ts
@Injectable()
export class PayrollService extends AbstractModel {
public collection = [];
constructor(private sS: SiteService,private stS: StorageService, private sws: SweetAlertService,
private accpService: AccountPayableService) {
super(sS, stS, sws);
}
}
Your comments and answered will be appreciated a lot.
Thanks
In Angular 2, we inject dependency via constructor of the class. In the above code, we have injected the “NumListServcie” dependency in constructor and made the “numList” object of “NumListServcie” type.
A cyclic dependency exists when a dependency of a service directly or indirectly depends on the service itself. For example, if UserService depends on EmployeeService , which also depends on UserService . Angular will have to instantiate EmployeeService to create UserService , which depends on UserService , itself.
Dependency Injection (DI) is a core concept of Angular 2+ and allows a class receive dependencies from another class. Most of the time in Angular, dependency injection is done by injecting a service class into a component or module class.
A hierarchical dependency injection system allows us to define different boundaries or scopes for our dependencies to run in and follows the component tree structure. By default, services registered to Angular are application wide but we can also create services that are isolated to a subset of components.
You can workaround circular dependencies by injecting Injector
instead of one of the services that cause the circular dependency
private payrollService:PayrollService;
constructor(/*private payrollService:PayrollService*/ injector:Injector) {
setTimeout(() => this.payrollService = injector.get(PayrollService));
}
In my case (Angular 4 in an Ionic project) even injecting the service just before it's usage (upon user interaction) was not working (Same "Can't resolve all parameters..." at startup).
What worked for me was to inject (and thus provide) the service by name.
So in my app module :
...
providers: [{provide: "MyServiceName", MyServiceClass}],
...
And when needed :
const myService: MyServiceClass = injector.get("MyServiceName");
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With