首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Angular 9-服务返回/列表包含数据,但*ngFor未显示任何内容

Angular 9-服务返回/列表包含数据,但*ngFor未显示任何内容
EN

Stack Overflow用户
提问于 2020-04-04 13:06:23
回答 3查看 245关注 0票数 0

这是我的代码示例,我正在尝试,并从我的API端得到了响应,但无法通过*ngFor显示。在category-LIS.COMPOMENT.TS的ngOnInit中,我调用了服务并获取了数据。还有一件事是我在这里使用了容器。

代码语言:javascript
运行
复制
    //category.service.ts
    getAll(): Observable<Response> {
        return this.httpClient
            .get<Response>(this.apiUrl + '/category/' + 1 + '/' + 10)
            .pipe(catchError(this.errorHandler));
    }

    //category-list.component.ts
    export class CategoryListComponent implements OnInit {
        @Input() pageSize = 4;
        categories: Category[] = [];
        constructor(
            public categoryService: CategoryService,
            private changeDetectorRef: ChangeDetectorRef
        ) {}

        ngOnInit() {
            this.categoryService.getAll().subscribe((data:Response) => {

                this.categories = data.data; 
                console.log(this.categories);
            });
        }
    } 
    //category-list.component.html
    <p>Category List</p>
    <ul>
      <li *ngFor="let data of categories">{{data.id}}</li>
    </ul>
//category.component.html
<div class="card-body">
    <app-category-list></app-category-list>
</div>  
//data

EN

Stack Overflow用户

发布于 2020-04-04 13:19:02

试着在html中输出这个变量,看看你首先得到了什么。

你的代码逻辑大体上看起来是正确的,除了关于可观察性的那部分(我不是专家)。尝试进行这些更改

代码语言:javascript
运行
复制
categories: Category[] = [];


this.categoryService.getAll().subscribe((data) => {
            this.categories = data.data;
});


<ul>
  <li *ngFor="let data of categories">{{data.id}}</li>
</ul>
票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61024020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档