亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以角度方式向 html 發送 REST 響應?

如何以角度方式向 html 發送 REST 響應?

臨摹微笑 2023-09-28 16:07:29
我能夠從我的應用程序中的 REST API 獲取數據,我能夠將數據打印到控制臺,但不知道如何顯示在 html 上,有人可以幫助我嗎?應用程序組件.tsimport { HttpClient } from '@angular/common/http';import { Component, Inject } from '@angular/core';import { Employee } from './employee';@Component({  selector: 'app-root',  templateUrl: './app.component.html',  styleUrls: ['./app.component.css']})export class AppComponent {  title = 'emp-data-example';  employees:Employee[]=[];  constructor(@Inject(HttpClient) private http:HttpClient){  }  ngOnInit(){    this.http.get("http://localhost:8080/api/all").subscribe(    response=>{      this.employees=response; // here is error because response is an Object and employees is an array    });    //resp.subscribe((data)=>{this.employees=data});      }}應用程序組件.html<div>  <table border="1">    <tr>      <th>Emp Id</th>      <th>Emp Name</th>      <th>Salary</th>    </tr>    <tr ngFor="let employee of employees">      <td>{{employee.id}}</td>      <td>{{employee.name}}</td>      <td>{{employee.department}}</td>      <td>{{employee.salary}}</td>    </tr>  </table></div>
查看完整描述

2 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

嘗試設置對 Employee 的響應類型并將其添加到數組中,如下所示:


    this.http.get("http://localhost:8080/api/all").subscribe(

    (response: Employee[]) =>{

      this.employees.push(...response);

    });


查看完整回答
反對 回復 2023-09-28
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

因為您在響應中獲取數組并且它來自員工類型,所以應該是這樣的:


this.http.get("http://localhost:8080/api/all").subscribe(

(response: Array<Employee>) =>{

  this.employees = response;

});


查看完整回答
反對 回復 2023-09-28
  • 2 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號