inject a class in angular code example
Example 1: create class angular
// to create a model class, we first need to create a new file
//f.e person.ts
export class Person {
constructor(
public name: string,
public lastName: string,
public age:number
) {}
}
//now that we have the model class we can create arrays that contain Person class elements
.
.
public people: Person[];
constructor(){
this.people = [
new Person ('Carla','Smith', 20 ),
new Person ('Carlos','Smith', 25 ),
new Person ('Andrea','Johnson', 23 ),
];
}
Example 2: how to inject service in component angular 6
you can inject service in components constructor:
constructor(public anyService: Service){}
Example 3: angular injector.create example
using Angular Injector
Internal class in @angular/core, StaticInjector, ReflectiveInjector
uses resolveAndCreate([ClassNames])
the resolve variable from above resolver.get(ClassName) will give the object.
All this is handled internally when we inform the angular with the providers