繁体   English   中英

在Angular 2中创建通用列表迭代器

[英]Creating a generic list iterator in Angular 2

我想包装一个项目列表,但我希望能够灵活地提供一个独特的模板。

<list [items]="someItems">
    <book-list-item><!-- Books, cars, dogs, whatever -->
    </book-list-item><!-- This will contain a unique template -->
</list>

ListItemComponent

import { Component, Input } from '@angular/core';

@Component({
    selector: 'list-item',
    template: `
        <div *ngFor="let item of items">
            <ng-content></ng-content><!-- Whatever book-list-item contains -->
        </div>`
})
export class ListItemComponent {
    @Input() items: Array<any>;
}

BookListItemComponent

import { Component } from '@angular/core';

@Component({
    selector: 'book-list-item',
    template: `
        <div class="row">
            <div class="col-xs-6">Books</div>
            <div class="col-xs-6">Rule</div>
        </div>`

})
export class BookListItemComponent {}

显然我会把书当成物品 ,但这只是一个例子。 我可以得到类似于这个工作的东西,但“Books Rule”只打印在最后一个list-group-item中(因为Bootstrap)。

我真的想要了解如何将通用组件传递给父组件,该组件将作为项列表中的项迭代通用组件。 有人能指出我正确的方向吗?

看看这篇文章 它的功能与您的需求非常相似。 你的主要挑战将是约束价值观。 你也可以看看Ben Nadel的文章 它与RC1有关,甚至他在评论中提到setLocal不再存在,你需要传递上下文。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM