簡體   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