0

I am build an Angular 4 app in which I am trying to use "model" classes like the following:

export interface UserI {
  uid?: string;
  birthdate: number;
  firstname: string;
}

export class User implements UserI {
  uid: string;
  birthdate: number;
  firstname: string;
  avatar: string;
  numberOfPictures$: Observable<number>;

  constructor(obj: any) {
    if (!obj.uid)
      throw new Error("User Class#constructor - No uid found for user.");

    this.uid = obj.uid;
    obj.firstname ? this.firstname = obj.firstname : this.firstname = '';
    obj.birthdate ? this.birthdate = obj.birthdate : this.birthdate = 0;
    obj.avatar ? this.avatar = obj.avatar : this.avatar = 'boy_1';
  }

}

I would like to use one of my provider to set the variable numberOfPictures$

How can I inject it? I am used to see Injection the constructor of components but I don't know how to proceed in this particular case.

Manuel RODRIGUEZ
  • 2,131
  • 3
  • 25
  • 53
  • Possible duplicate of [How to inject Service into class(not component)](https://stackoverflow.com/questions/41432388/how-to-inject-service-into-classnot-component) – ConnorsFan Dec 18 '17 at 14:48
  • maybe create a function in your provider than return a new User (and init by this method) ? So when needed a new User, construct it throw your provider, not directly. – milcaepsilon Dec 18 '17 at 14:52

0 Answers0