角点函数如何创建和应用ngFor

时间:2019-01-28 11:59:23

标签: angular

我正在使用Angular获取响应表单API

我的回应

{"Test":
[{"username":"Test","status":"D"},
{"username":"Test","status":"P"},
{"username":"Test","status":"M"]}

service.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';

@Injectable({
  providedIn: 'root'
})
export class ReportService {
  uri = 'http://localhost:3000/api/url';
  token = localStorage.getItem('token');
  myData : any;
  Object = Object;
  constructor(private http: Http) { }
  getBusinesses(){
    let headers = new Headers({ 'x-access-token': `${this.token}` });
    let options = new RequestOptions({ headers: headers });
    return this.http.get(`${this.uri}`, options)
    .subscribe(
      result => {
        this.myData = result.text();
        console.log(result.text());
      },
      error => {
        console.log(error.text());
      }
    );
  }
}

我不知道如何在谷歌上打印它,他们正在使用管道函数,我不知道如何创建管道函数并对其进行循环

谁能解释为什么管道函数需要循环数据

1 个答案:

答案 0 :(得分:0)

不是将myData设置为任何类型,

myData : any;

使其成为界面,

myData : {
    Test : {
        username : string,
        status   : string
    }[]
} = { Test : [] }

在您的HTML中,

<div *ngFor="let data of myData.Test">
    Username : {{data.username}}, Status : {{data.status}}
</div>

这将显示您的数据,

  

用户名:测试,状态:D

     

用户名:测试,状态:P

     

用户名:测试,状态:m

     

...