2017-10-12 69 views
2

我是新的角度,並試圖找出它是如何工作的。我試圖從API中爲我的表提取數據。角度Http獲取方法表

import { Component } from '@angular/core'; 
import { Http } from '@angular/http'; 
import 'rxjs/add/operator/map'; 
@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
class AppComponent { 
    people: any; 
    constructor(http: Http) { 
    http.get('https://jsonplaceholder.typicode.com/todos') 
     .map(res => res.json()) 
     .subscribe(people => this.people = people); 
    } 
} 

interface people { 
    userId:number; 
    id:number; 
    title:string; 
    completed:boolean; 

而這個HTML代碼塊

<tr *NgFor="let people of peoples"> 
       <td>{{people.userId}}</td> 
       <td>{{people.id}}</td> 
       <td>{{people.title}}</td> 
       <td>{{people.completed}}</td> 

,我做錯了嗎?任何建議非常有幫助。

+2

'* NgFor =「...」' - >'* ngFor =「...」' –

+0

我強烈建議在開始編程之前閱讀更多內容 –

回答

0

您拼錯了指令,它是* ngFor,並且您正在嘗試引用名爲peoples的變量,因爲在您命名爲people的組件中不存在該變量。

檢查控制檯是否將來會出現錯誤,而且還應該閱讀更多內容,因爲您的代碼有很多不良做法。嘗試使用服務並將您的http請求放在那裏,並簡單地將它們注入到組件中。