There are multiple classes that inject the TermsExchangeService
:
import {TermsExchangeService} from './terms-exchange.service';
@injectable()
export class TermsExchangeComponent {
constructor(
private termsExchangeService: TermsExchangeService
) {}
}
and
import {TermsExchangeService} from './terms-exchange.service';
@injectable()
export class PurchaseMainComponent {
constructor(
private termsExchangeService: TermsExchangeService
) {}
The service is used in both PurchaseMainComponent
and TermsExchangeComponent
@injectable()
export class TermsExchangeService {
constructor() {
debugger; // called 2 times!!!
}
}
I am using autoBindInjectable
:
var simpleContainer = new Container({ autoBindInjectable: true })
When retrieving components in a loop, the constructor of TermsExchangeService
is called multiple times:
for (let item = 0; item < components.length; item++) {
const container = simpleContainer.get(components[item]);
}
Why is an object created in every component? And how can normal injections be done?