4

I have been stuck with the basic task, of trying to update an array and output the according values. This has been affecting the whole application.

I reduced and simplified my problem to the following. First I created the following model:

export class Recipe {
  public name: string;
  public description: string;
  public imagePath: string;


constructor(name: string, desc: string, imagePath: string) {
  this.name = name;
  this.description = desc;
  this.imagePath = imagePath;
  }
}

I then proceeded to adding content to the recipes component:

import { Component, OnInit } from '@angular/core';

import { Recipe } from '../recipe.model';


@Component({
   selector: 'app-recipe-list',
   templateUrl: './recipe-list.component.html',
   styleUrls: ['./recipe-list.component.css']
 })

 export class RecipeListComponent implements OnInit {

  recipes: Recipe[] = [
    new Recipe('NAME', 'DESC','URL1'),
    new Recipe('NAME2', 'DESC2','URL2')
   ];

   constructor() {
   }

   ngOnInit() {
   }
}

I then use *ngFor in the following manner:

 <a 
  href="#"
  class="list-group-item clearfix"
  *ngFor="let recipe of recipes">
  <div">
    <h4 class="list-group-item-heading">{{ recipe.name }}</h4>
    <p class="list-group-item-text">{{ recipe.description }}</p>
  </div>
  <span>
    <img
      src="{{ recipe.imagePath }}"
      alt="{{ recipe.name }}"
      class="img-responsive"
      style="max-height: 50px;">
  </span>
</a>

I have looked at other questions such as this, however I was unable to implement solutions accordingly.

I am unable to see changes on the browser when I change my array values.

How can I add new recipes and make them visible in the browser using *ngFor ?

Any pointers on what core functionality I am missing would be appreciated.

4
  • Did you try this.recipes.push(new Recipe('NAME2', 'DESC2', 'URL2')) in your component? Commented Sep 23, 2017 at 9:29
  • Yes, no luck yet however. It seems that I am missing something basic in this case. Commented Sep 23, 2017 at 9:43
  • Could you add more code relating to RecipeListComponent? Maybe there's some issue with recipes array reference... Commented Sep 23, 2017 at 9:59
  • Now I can reproduce and fix it... please, see my answer Commented Sep 23, 2017 at 10:21

2 Answers 2

4

I created working demo on your issue: https://plnkr.co/edit/u2bq5T?p=preview

Template:

 <button (click)="addRecord()">Add a record</button>
 <a 
  href="#"
  class="list-group-item clearfix"
  *ngFor="let recipe of recipes">
  <div>
    <h4 class="list-group-item-heading">{{ recipe.name }}</h4>
    <p class="list-group-item-text">{{ recipe.description }}</p>
  </div>
  <span>
    <img
      src="{{ recipe.imagePath }}"
      alt="{{ recipe.name }}"
      class="img-responsive"
      style="max-height: 50px;">
  </span>
</a>

Class:

export class App {
  index:number = 2;
  recipes: Recipe[] = [
    new Recipe('NAME', 'DESC','URL1'),
    new Recipe('NAME2', 'DESC2','URL2')
   ];

  constructor() {
  }

  addRecord() {
    this.index++;
    this.recipes.push(new Recipe('NAME' + this.index, 'DESC'+ this.index,'URL' +this.index));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

I would do it like this:

recipes : Recipe[] = [];
recipe : Recipe;

ngOnInit() {
 this.recipes.push(this.recipe = {name: 'asd'}, 
                   this.recipe = {name: 'asf'});
 console.log(this.recipes);
}

I think it's a bit less error prone, because if you change any properties of Recipe, this would help to debug, because you see what value is assigned to property (name: 'asd')

Comments

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.