簡體   English   中英

來自 web api 的數據,它不在 Html 頁面中顯示

[英]Data from web api and it's not Display in Html page

我嘗試創建一個簡單的 angular 應用程序。 我正在嘗試從 web api 獲取數據。 問題是數據到達前端(console.log(DataObeject)。

這是我的 service.ts

     employerApiUrl = 'http://localhost:63858/api/';

    //private url: UrlConst;
    constructor(private _httpClient: HttpClient) { }

    // get all employee list

    getAllEmployee(): Observable<Employer>  {
        return this._httpClient.get<Employer>(this.employerApiUrl + 'Employe/getAllEmploye');
    }
}

這里是我的組件 ts

export class EmployerVeiwComponent implements OnInit {

  EmployerList;
  constructor(private _employerDetails: EmployerService) { }

  ngOnInit() {
    this.getEmployee();
  }

  // getting employee data from service 
  getEmployee() {
    this._employerDetails.getAllEmployee()
      .subscribe(
        employerList => employerList = this.EmployerList);
  }
}

這里是我的 html

<table class="table table-condensed">
    <thead>
      <tr>
        <th>Employer ID</th>
        <th>Firs Name</th>
        <th>Last Name <button>&#x2191;</button></th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor = "let employer of EmployerList">
        <td>{{ employer.ID }}</td>

      </tr>
    </tbody>
  </table>

這里的問題

在此處輸入圖像描述

這里是控制台結果

在此處輸入圖像描述

它在來自 API 的記錄數量中創建條形圖。

問題很少

(i)您需要將數組初始化為

EmployerList : any =[];

(ii)將結果分配給EmployerList

getEmployee() {
    this._employerDetails.getAllEmployee()
      .subscribe(
        employerList => this.EmployerList = employerList );
}

(iii)另外,它應該是id而不是模板中的ID

<tr *ngFor = "let employer of EmployerList">
     <td>{{ employer.id}} : {{employer.fname}}</td>
</tr>
  @CrossOrigin(origins = "*")  add this in your controller

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM