首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换组件并仍然使用ngFor

转换组件并仍然使用ngFor
EN

Stack Overflow用户
提问于 2017-02-17 07:00:20
回答 1查看 406关注 0票数 2

我试图模拟从API返回的数据数组上的分页。这对我来说是完美的,但是我需要做的是嵌套另一个组件,它在其中使用ngFor,它的ngFor数据是从被排除的组件中传递下来的。

如果我在转换组件中运行调试器,我可以看到我的完整数组即将出现,我也可以看到它正在拼接,并且创建了一个新的对象,其中包含了我已经变异的数据。

但是,当我尝试使用ngFor呈现组件并传入新数组时,不会呈现任何内容。

我还向该组件添加了调试器,它似乎根本不被触发,因为调试器甚至不会触发。

我的代码如下:

object-transclusion.component.ts

代码语言:javascript
复制
import {Component, OnInit, Input} from '@angular/core';

@Component({
    selector: 'app-object-transclusion',
    template: '<ng-content></ng-content>'
})

export class ObjectTransclusionComponent implements OnInit {

    @Input() items: any;

    public pageNo: number = 1;
    public paginatedItems: string[];

    private results_per_page: number = 1;
    private totalPages: number;

    ngOnInit() {
        this.totalPages = this.getMaxPages();
        this.updatePaginationState();
    }

    public nextPage(): void {
        if (this.pageNo < this.totalPages) {
            this.pageNo++;
            this.updatePaginationState();
        }
    }

    public prevPage(): void {
        if (this.pageNo > 1) {
            this.pageNo--;
            this.updatePaginationState();
        }
    }

    private updatePaginationState(): void {
        this.paginatedItems = [
            ...this.items.slice(
                (this.pageNo - 1) * this.results_per_page, this.pageNo * this.results_per_page
            )
        ];
    }

    private getMaxPages(): number {
        return Math.ceil(this.items.length / this.results_per_page);
    }
}

event-table.component.pug

代码语言:javascript
复制
.event-table-container
    life-event-table-header
    app-object-transclusion([items]='events')
        life-event-table-row(*ngFor='let event of paginatedItems', [event]='event', [canModifyLifeEvents]='canModifyLifeEvents')

如何在transclusion组件中对数据进行排序,使其可用于通过ng内容呈现的组件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-17 07:24:41

好的,所以看起来解决方案是向

代码语言:javascript
复制
app-object-transclusion(#transclude [items]='events')

这允许我使用以下方法输出数据

代码语言:javascript
复制
*ngFor='let event of transclude.paginatedItems'

不像我喜欢的那样优雅,但它正在起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42291384

复制
相关文章

相似问题

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