2

This is my code

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

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

export class JobcompletedComponent {

  name: string;
  email: string
  address: string;
  hobbies: string[];
  showHobbies: boolean;

  constructor(){
  this.name = "john doe",
  this.email = "[email protected]",
  this.address = "ukay perdana"
  this.hobbies= ['music','movies','sport'];
  this.showHobbies = true;
  }
  tooggleHobbies(){}
}

my template

<h2>Hello {{ name }}</h2>
<h2>email : {{ email }}</h2>
<h2>address : {{ address }}</h2>
<h2>hobby</h2>
<ul>
  <li ngFor="let hobby of hobbies">{{ hobby }}</li>
</ul>

But the ngFor does not display any data:

Result

I've tried many style of code but none of them works for me:-

    <ul>
      <li *ngFor="let hobby of hobbies">{{ hobby }}</li>
    </ul>

<ul>
      <li *ngFor="#hobby of hobbies">{{ hobby }}</li>
    </ul>

<ul>
          <li ngFor="#hobby of hobbies">{{ hobby }}</li>
        </ul>

What is wrong with my code? And this is my angular version

@angular/cli: 1.0.0-beta.31
node: 6.9.2
os: win32 x64
@angular/common: 2.4.7
@angular/compiler: 2.4.7
@angular/core: 2.4.7
@angular/forms: 2.4.7
@angular/http: 2.4.7
@angular/platform-browser: 2.4.7
@angular/platform-browser-dynamic: 2.4.7
@angular/router: 3.4.7
@angular/cli: 1.0.0-beta.31
@angular/compiler-cli: 2.4.7
5
  • 1
    I just tried <li *ngFor="let hobby of hobbies">{{ hobby }}</li> and it worked... Make sure you're recompiling your TypeScript and reloading your server... If that fails, check your console for errors. Commented Feb 22, 2017 at 4:02
  • Any errors? pic shows one dot so an li is being displayed right? What do elements look like in dev tools? Above the list add <pre>{{ hobbies | json }}</pre> to make sure the context has the data you expect Commented Feb 22, 2017 at 4:03
  • <ul> <li *ngFor="let hobby of hobbies">{{ hobby }}</li> </ul> Commented Feb 22, 2017 at 5:39
  • i don't have any errors on my console.. Commented Feb 22, 2017 at 7:07
  • @uetoaya Please recreate the issue in a plunker :) Commented Feb 22, 2017 at 13:39

3 Answers 3

1

Please check the image , It works for me.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Avoid link only answers.
wow... i just notice that at the bottom of my code does not have @NgModule.. but when i put NgModule i got an error said "Cannot find name 'NgModule'.)"
import ngModule see top imports
0

It's

<h2>Hello {{ name }}</h2>
<h2>email : {{ email }}</h2>
<h2>address : {{ address }}</h2>
<h2>hobby</h2>
<ul>
  <li *ngFor="let hobby of hobbies">{{ hobby }}</li>
</ul>

*ngFor, *ngIf, *ngSwitchCase, structural directives needs an * before the directive.

1 Comment

<ul> <li [ERROR ->]*ngFor="let hobby of hobbies">{{ hobby }}</li> </ul>
0

You can put *ngFor on ul.

 <ul *ngFor="let hobby of hobbies">
  <li>{{hobby}}</li>
</ul>

Plunker : https://plnkr.co/edit/9i0lwxaJSmnCVcZVCen1?p=preview

1 Comment

<ul> <li [ERROR ->]*ngFor="let hobby of hobbies">{{ hobby }}</li> </ul>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.