0

So I'm learning angularjs 2 and having trouble understanding the uses of service and getting it to work with my app.

film.services.ts

   import { Injectable } from "@angular/core";

import { Film } from './film';
import { FILMS } from './mock-films';

@Injectable()
export class FilmService {
  getFilms(): Promise<Film[]> {
    return Promise.resolve(FILMS)
  }
}

mock-films

import { Film } from './film';

//storing the data in array
export const FILMS: Film[] = [
  {id:11, title: 'pokemon', summary:'great'},
  {id:12, title: 'naruto', summary: 'good'},
  {id:13, title: 'bleach', summmary: 'meh'},
  {id:14, title: 'one piece', summary: 'not bad'}
];

film-list.components.ts

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable'
import { FilmService } from '../film.service';
import { Film } from "../film";

@Component({
  template: `
  <h1>List of Films</h1>
  <p>Films:</p>
  <ul>
    <li *ngFor="let film of films ">
     <a [routerLink]="['/films', film.id]">{{film.title}}</a>
      </li>
  </ul>
  <p *ngIf="films.length > 3">There are many vietnamese films to learn about!</p>
`,
  // important when adding to service other crashes
  providers: [FilmService]
})

//Component class
export class FilmListComponent implements OnInit {
  public films: Film[];
  constructor(private filmService: FilmService) {}

  getFilms(){
    this.filmService.getFilms().then((films: Film[]) => this.films = films);
  }

  ngOnInit():any {
    this.getFilms()
  }
}

so my goal is getting the data from my mock-films into my film list but it is not showing up for some reason. It probably the film-list.component.ts but not sure where the error is.

1
  • May I know what error you're getting? Commented Nov 2, 2016 at 5:14

1 Answer 1

1

fix in here

  public films: Film[]=[];

OR

public films: Film[];
  constructor(private filmService: FilmService) {}
     getFilms(){
        this.flims=[];
        this.filmService.getFilms().then((films: Film[]) => this.films = films);
      }

film-list.components.ts

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable'
import { FilmService } from '../film.service';
import { Film } from "../film";

@Component({
  template: `
  <h1>List of Films</h1>
  <p>Films:</p>
  <ul>
    <li *ngFor="let film of films ">
     <a [routerLink]="['/films', film.id]">{{film.title}}</a>
      </li>
  </ul>
  <p *ngIf="films.length > 3">There are many vietnamese films to learn about!</p>
`,
  // important when adding to service other crashes
  providers: [FilmService]
})

//Component class
export class FilmListComponent implements OnInit {

  public films: Film[];
  constructor(private filmService: FilmService) {}

  getFilms(){

    this.flims=[];//fix in here
    this.filmService.getFilms().then((films: Film[]) => this.films = films);
  }

  ngOnInit():any {
    this.getFilms()
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, it work. can you explain why that works so I have a good understanding for it?
public films: Film[]=[]; you need to tell what is this like this eg.public flim:string="";if you declare like this public films :Flim[] this will undefined and give you error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.