0

I am facing an issue in my angular 2 application where the data I save in service is not retrieved in component and then the view(not sure which one is the issue here). I am using an observable and subscribing to it. Below are my files.

Recipe.service.ts:

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

  recipes : Array<Recipes> = new Array(20);

  getRecipes(): Observable<Recipes[]>{

    return this.http.get('my api here')
      .map(this.extractData.bind(this))
  }

  private extractData(res: Response) {
    let body = res.json();

    let total_no_of_recipes = body.pagination.count;
    let no_of_recipes;

    for(no_of_recipes = 0; no_of_recipes < total_no_of_recipes; no_of_recipes++) {
      this.recipes[no_of_recipes] = body.data[no_of_recipes].embed_url;
    }
    console.log(this.recipes); ---> Data is printed in console with no problem here
    return this.recipes;
  }

}

app.component.ts:

export class AppComponent implements OnInit{
  observableRecipes: Observable<Recipes[]>;
  recipes: Recipes[];

  constructor(private recipeservice: RecipeService){}

  ngOnInit(){}

  onSubmit(){
    this.observableRecipes = this.recipeservice.getRecipes();
    this.observableRecipes.subscribe(
      recipes => { this.recipes = recipes }
    );
  }
}

app.component.html:

<span>
    <a *ngFor="let recipe of recipes"> 
        {{ recipe.image }}    ---> error is from these two lines where I am accessing the value
        <iframe src="{{ recipe.image }}" style="width: 280px; height: 170px;"></iframe>
    </a>
</span>

Recipe.model.ts:

export class Recipes{
  public iamge: string;

  constructor(image: string){
    this.image = image;
  }
}

Can you please help as I am not able to understand why it is not receiving the data.

venky4t
  • 51
  • 7
  • Possible duplicate of [Unsafe value used in a resource URL context with Angular 2](https://stackoverflow.com/questions/37927657/unsafe-value-used-in-a-resource-url-context-with-angular-2) – match Feb 04 '18 at 16:25
  • @match I tried the URL way, but it didn't work. Since, I am receiving an Observable from the service in the component(app.component.ts) I subscribed on to it. But I am not sure why it is not receiving data after it. Can you see any other issue here. – venky4t Feb 04 '18 at 16:56
  • I think the main point from the answers there is you need to set `[src]="recipe.image"` not `src="{{recipe.image}}"` – match Feb 04 '18 at 17:23
  • I tried this. But it loads the whole page inside the – venky4t Feb 04 '18 at 17:30
  • 1
    Are you missing the '@Injectable()' notation before the service class? – Hany Feb 04 '18 at 17:33
  • No. I did include the '@Injectable'. Just didn't keep in here. Updated. – venky4t Feb 04 '18 at 17:34
  • 1
    I don't see a definition for 'Recipes' used in the '*ngFor' – Hany Feb 04 '18 at 17:40
  • @Hany I changed it to "recipes" value which comes from this.observableRecipes.subscribe( recipes => { this.recipes = recipes } ); It still didn't work. Is this not a correct way? – venky4t Feb 04 '18 at 17:45
  • 1
    Now enclose it by '
    and retry
    – Hany Feb 04 '18 at 17:49
  • I did it. Still the same issue. – venky4t Feb 04 '18 at 18:11
  • I modified code a bit to see whether array was retrieved in app.component.ts and the data does show up in console. Here is the code. this.observableRecipes.subscribe( (recipes: Recipes)=> { if(recipes == null){ this.statusMessage = 'gifs value is empty'; } else { this.recipes = recipes; console.log(this.recipes); } }); I am not sure why it was not accessible in

    {{ recipe.image }}

    – venky4t Feb 04 '18 at 20:30

0 Answers0