javascript - 带有 *ngFor 的 Angular 视图不渲染数据
问题描述
我是 Angular 的新手,但熟悉 Javascript。
我正在为演示设置 CRUD,但无法让此列表循环遍历从我们的 API 返回的数据。
数据加载正常,我可以在控制台中看到它。但是*ngFor
下面没有呈现任何<li>
s。
pages.component.html:
<div class="row">
<div class="col-sm-4">
<p>Pages</p>
<ul>
<li *ngFor="let page of pages">
{{page.Name}}
</li>
<li>this shows up.</li>
</ul>
</div>
</div>
pages.component.ts:
import { Component, OnInit } from '@angular/core';
import { PageService } from '../page.service';
import { Page } from '../page';
@Component({
selector: 'app-pages',
templateUrl: './pages.component.html',
styleUrls: ['./pages.component.css']
})
export class PagesComponent implements OnInit {
pages: Page[];
getPages(): void {
this.pageService.all().subscribe(function(pages) {
console.log(pages);
this.pages = pages;
}
}
constructor(private pageService: PageService) { }
ngOnInit() {
this.getPages();
}
}
就像我说的,数据显示在控制台中,所以我认为它必须在视图中。
解决方案
尝试这个:
在 ts 文件中:
getPagesFromView() {
If (this.pages.length > 0) {
return this.pages;
}
}
推荐阅读
- walmart-api - 来自 WalmartLabs API 的零星 504 网关超时错误
- r - 如何使用 data.table 根据条件计算列总和?
- php - 如何获取用户的 IP,然后在缓存的 amp 表单上定位?
- jquery - Fullcalendar 资源未在版本 4 中显示
- python - 将 VBA 转换为 Python - 文件结构问题
- python - 如何使用正则表达式遍历字符?
- javascript - MongoDB,如何创建一个空集合?
- scala - 将多个(任意数量)火花 DataFrame 列连接成一个“|” 分隔字符串
- kubernetes - 使用 operator-sdk 或一般部署的 Kubernetes 中的控制器的协调时间是多少?您可以设置自定义时间进行侦察吗?
- reactjs - 如何修复 Uncaught (in promise) SyntaxError: Unexpected end of JSON input?