【问题标题】:Angular 6 does not display data retrieved from Asp Net Web API 2.2Angular 6 不显示从 Asp Net Web API 2.2 检索到的数据
【发布时间】:2020-02-27 16:50:17
【问题描述】:

我的问题的简要说明:

我有一个 Angular 应用程序,它使用 Asp Net Core Web API 2.2 运行。一般来说,这并不复杂,主要是 CRUD 应用程序,对于比我更高级的 Angular 用户来说,问题也不是火箭科学问题。

Web API 托管在 IIS 上,具有正确的数据库连接。这是正确的,因为样本数据被放入数据库,并且可以在 Postman 结果中看到它被检索。

虽然,在客户端,数据不会显示。服务和组件代码如下,我还包括控制台反馈。生成应显示数据的表,即使具有相应的行数。唯一的问题是数据丢失。我怀疑正确的 JSON 格式。仅仅因为它是我生命中第一个此类项目,我将 Angular 和 .Net 结合起来,我对类似问题缺乏经验,我很想得到一些提示和帮助。

相关代码:

对于 Angular 服务,我有:

  url = 'http://localhost:8080/api/clients/';

  constructor(private http: HttpClient) { }

  getClients(): Observable<Array<Client>> {
    return this.http.get<Array<Client>>(this.url );
  }

对于组件(我在初始化时调用 getClients)

 clients:  Array<Client> ;       
 client: any;             

  getClients() {  
    this.clientService.getClients().subscribe(data => {
      this.clients = data ;
      console.log(data); 
    });

  }

under and over 'tutaj' word ('here' in polish I try console.log array)

matching num of rows in table from api

Postman message

【问题讨论】:

  • 你在 html 端的代码是什么?你需要在html端设置数据太正确。
  • 你能在重新加载后发布你的网络快照吗?
  • 您如何将this.clients 绑定到您的Angular 标记?您是否还可以显示执行console.log(data)行时显示的内容。
  • 我的提示:使用Nsag 生成您的服务
  • @Angelo,console.log(data) 现在显示两个数组,格式如下 Array(2) 0: {id: 7...

标签: c# asp.net angular asp.net-web-api


【解决方案1】:

我已经解决了,问题确实出在 html 代码中。我有一些与登录系统相关的代码,它正在检查登录用户的角色,它导致了错误。 不过感谢您的贡献!

F。 Bekir Yücel,序列化模型不会改变任何东西。

getClients() {  
    this.clientService.getClients().subscribe(data => {
      this.clients = data ;
      console.log(data); // this one displays daya
      console.log("Test"); //this also is displayed
      console.log(this.clients[0].Id); // this is undefined
    });

Html 部分如下所示:

<td id="id">{{client.cardId}}</td>

<td>
  <div *ngIf="client.id != editRowId">
    <p>{{client.name}}</p>

  </div>

直到最后一栏都是这样。

【讨论】:

    猜你喜欢
    • 2015-07-23
    • 2019-07-08
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 2019-03-07
    相关资源
    最近更新 更多