2017-10-12 90 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请求放在那里,并简单地将它们注入到组件中。