-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
33 lines (28 loc) · 807 Bytes
/
Copy pathindex.ts
File metadata and controls
33 lines (28 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class HelloSrv {
sayHello(): void {
console.log('hi everyone.....');
}
}
class Component {
constructor(public readonly helloSrv: HelloSrv) {}
}
// Angular DI
class Injector {
private _container = new Map();
constructor(private _providers: any[] = []) {
_providers.forEach((service) => {
this._container.set(service, new service());
});
}
get(service: any) {
const serviceInstance = this._container.get(service);
if (!serviceInstance) {
throw Error(' no provider found');
}
return serviceInstance;
}
}
//Add to providers
const injector = new Injector([HelloSrv]);
const component = new Component(injector.get(HelloSrv));
component.helloSrv.sayHello();