Answer the question
In order to leave comments, you need to log in
How to inject a service into a data class?
With DI in components, everything is clear - angular automatically injects them if they are specified in the constructor. But what if the service is needed in a data class, for example, some
class user {
id: number;
name: string;
constructor(id: nubmer, name: string) {
this.id = id;
this.name = name;
}
get address(){
/* someService */
}
}
let injector = ReflectiveInjector.resolveAndCreate([UserService]);
User.userService = injector.get(UserService);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question