2017-02-22 61 views
2

这是我的代码angular2 ngFor不显示数据

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(){} 
} 

我的模板

<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不显示任何数据:

Result

我试着的代码,但他们没有很多风格的作品对我来说: -

<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> 

我的代码有什么问题?这是我的角度版本

@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 
+1

我只是想'

  • {{爱好}}
  • '和它的工作...确保你重新编译打字稿并重装服务器。如果失败,请检查控制台是否有错误。 –

    +0

    有没有错误?图片显示一个点,所以“li”显示正确?开发工具中元素的外观如何?上面的列表中添加''

    {{ hobbies | json }}
    以确保上下文有你期望 –

    +0

    • 数据{{爱好}}

    回答

    0

    这是

    <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,结构指令的指令之前需要一个*

    +1

    他说,他已经试过了。 –

    +0

    • ] * ngFor = “让的爱好嗜好”> {{爱好}}
    MunirohMansoor

    2

    请检查图像,这对我的作品。

    enter image description here

    +0

    避免链接 “让的爱好嗜好”答案。 –

    +0

    哇......我只是注意到,在我的代码的底部没有@NgModule ..但是,当我把NgModule我得到一个错误说“无法找到名为‘NgModule’)。” – MunirohMansoor

    +0

    进口ngModule看到顶部进口 –