2017-02-22 59 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看到頂部進口 –