I have a real scenario in a real project where I need 2 services to access the properties and/or methods of each other. I\'m not an Angular expert so is it possible?
I agree with the solution proposed by basarat. Another workaround would be to initialize the instances outside DI and provide them as value like
One service needs to be modified to be able to create an instance without providing the other service as dependency:
@Injectable()
export class FirstService {
foo: string = 'abc';
secondService: SecondService
constructor() {
//this.foo = this.foo + this.secondService.bar;
}
init(secondService:SecondService) {
this.foo = this.foo + secondService.bar;
}
}
Then create the instances imperatively and provide them as value
let firstService = new FirstService();
let secondService = new SecondService(firstService);
@Component({
selector: 'my-app',
template: 'Hello world!
',
providers: [
provide(FirstService, {useFactory: () => {
firstService.init(secondService);
return firstService;
}}), provide(SecondService, {useValue: secondService})]
})
...
Plunker example