【问题标题】:Data is shown in the console but not in the website Angular数据显示在控制台中,但不在网站 Angular 中
【发布时间】:2021-11-29 19:53:48
【问题描述】:

这是我的 ngOnInit 方法:

async ngOnInit() {
    
    return this.http.get(this.apiserver.apiUrl + 'negocios/listadoTodos/?negocio=1').subscribe(async (todos:any) => {
        this.detalles = todos;
        console.log(this.detalles);
        
      });
    
}

我的控制台: enter image description here

我的 HTML:

<div class="text-lg font-bold leading-none" *ngFor="let item of detalles">{{item.nombre}} - {{item.sector}}</div>

我正在尝试使用我的本地主机上的 api,我想知道为什么它在控制台上可以工作,但在前端 html 中却没有。

【问题讨论】:

  • 你能从 todos 中显示 console.log 吗?
  • 能展示整个组件的html和ts代码吗?

标签: javascript angular typescript api


【解决方案1】:

您的 API 可能会花时间加载数据,因此当 API 调用获取数据时,它将在控制台中打印数据,但在此之前您的 HTML 会在浏览器上呈现,因此您需要在 *ngFor 之前使用 *ngIf。

<div *ngIf="detalles.length">    
    <div class="text-lg font-bold leading-none" *ngFor="let item of detalles"> 
         {{item.nombre}} - {{item.sector}}
    </div>
</div>

【讨论】:

  • 这对我很有用,非常感谢你,我花了一整天的时间寻找这个问题的答案
猜你喜欢
  • 1970-01-01
  • 2021-10-23
  • 2020-03-11
  • 1970-01-01
  • 2023-02-13
  • 2019-04-11
  • 1970-01-01
  • 2022-08-03
  • 2019-08-15
相关资源
最近更新 更多