Search code examples
angularngfor

angular ngFor list item not showing


I want to display a simple ul element with some li in it. For this I created this component:

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

@Component({
  selector: 'skin',
  templateUrl: './skin.component.html',
  styleUrls: ['./skin.component.css']
})
export class SkinComponent {

  skins: [{
    id: 1,
    className: 'color-circle blue light',
    skinName: 'Blue light'
  }, {
    id: 2,
    className: 'color-circle orange light',
    skinName: 'Orange light'
  }, {
    id: 3,
    className: 'color-circle lime light',
    skinName: 'Lime light'
  }, {
    id: 4,
    className: 'color-circle purple light',
    skinName: 'Purple light'
    },
  {
    id: 5,
    className: 'color-circle teal light',
    skinName: 'Teal light'
  }
  ];

  constructor() { }

}

And this is the corresponding html:

  <ul class="schemes" style="float: left">
    <li *ngFor="let skin of skins">
      <span class="{{skin.className}}"></span>
      <span class="color-name">{{skin.skinName}}</span>
    </li>
  </ul>

But if I compile the app, there is no list item. The ul element gets rendered of couse, but without li items.

In the browser I see this:

enter image description here

What is here the issue?

Thanks.


Solution

  • That is because you have set your list value as type instead of assigning them as value inside your variable.

    From this:

    skins: [{ ... }]
    

    Change it to this

    skins = [
      {
        id: 1,
        className: 'color-circle blue light',
        skinName: 'Blue light'
      }, {
        id: 2,
        className: 'color-circle orange light',
        skinName: 'Orange light'
      }, {
        id: 3,
        className: 'color-circle lime light',
        skinName: 'Lime light'
      }, {
        id: 4,
        className: 'color-circle purple light',
        skinName: 'Purple light'
        },
      {
        id: 5,
        className: 'color-circle teal light',
        skinName: 'Teal light'
      }
    ];