Use service inside model class

I created a model class for movies in my project, which contains the following

//movie/model.ts
export class Movie {

  title:string;
  overview:string;
  id:string;
  seasons:any[];

  constructor(obj: any) {

        this.id = obj.id;
        this.overview = obj.overview;
        this.title = obj.title;

    }
}

I also made a service that forces all http calls to retrieve data from the api.

//api.service.ts
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/Rx';

import {Movie} from './movie/model';

@Injectable()
export class ApiService {

  constructor(private http: Http) {}

  getMovie(id):{

    let url = this.Api_Url + '/movies/' + id + "?extended=full,images";

    return this.http.get(url, { headers: this.headers })
      .map(d => d.json())
      .map(d => new Movie(d))
      .toPromise()
  }

  getActors(){...}

Everything works fine, I provide one instance of the api service in my bootstrap, and I can use it in each component to get the movie. Although I would like to have this instance of the api service in my class, but I don't know how to tell angular to provide it. This is what I want and what I tried.

//movie/model.ts

import {ApiService} from "../api.service";
export class Movie {

  title:string;
  overview:string;
  id:string;
  seasons:any[];

  constructor(obj: any, private api: ApiService) {}

  getActors(): { 
    this.api.getActors()
    //api is null 
  }
}

@Inject(api) new, , http. , angular api , , es6? -? angular 1, , .

+4
1

, - Angular2, . , .

, :

@Injectable()
export class ApiService {
  constructor(private http: Http) {}

  getMovie(id):{
    let url = this.Api_Url + '/movies/' + id + "?extended=full,images";

    return this.http.get(url, { headers: this.headers })
      .map(d => d.json())
      .map(d => new Movie(d, this)) // <----
      .toPromise()
  }

  getActors(){...}
+2
source

All Articles