代码之家  ›  专栏  ›  技术社区  ›  Ryoush

表的角度Http get方法

  •  2
  • Ryoush  · 技术社区  · 8 年前

    我是angular的新手,我试着弄清楚它是如何工作的。我试图从API中为我的表提取数据。

    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>
    

    我哪里做错了?任何建议都非常有用。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Christian    8 年前

    您将指令拼写错误,它是*ngFor,并且您试图引用名为people的变量,该变量不存在,因为在组件中您将其命名为people。

    以后检查控制台是否有错误,您还应该阅读更多内容,因为您的代码有很多错误做法。试着利用服务,把你的http请求放在那里,然后简单地把它们注入到你的组件中。