简体   繁体   English

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

[英]Creating a generic list iterator in Angular 2

I want to wrap a list of items, but I want the flexibility of supplying a unique template. 我想包装一个项目列表,但我希望能够灵活地提供一个独特的模板。

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

ListItemComponent 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 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 {}

Obviously I'd be passing books as items , but this is just an example. 显然我会把书当成物品 ,但这只是一个例子。 I can get something similar to this working, but "Books Rule" only prints out in the last list-group-item (because Bootstrap). 我可以得到类似于这个工作的东西,但“Books Rule”只打印在最后一个list-group-item中(因为Bootstrap)。

I'm really looking to understand how to pass a generic component to parent component that will iterate over the generic component as an item in a list of items. 我真的想要了解如何将通用组件传递给父组件,该组件将作为项列表中的项迭代通用组件。 Can someone point me in the right direction? 有人能指出我正确的方向吗?

Take a look at this article . 看看这篇文章 It does something very similar to what you need. 它的功能与您的需求非常相似。 Your main challange will be binding the values. 你的主要挑战将是约束价值观。 You can also Look at Ben Nadel's article . 你也可以看看Ben Nadel的文章 It relates to RC1 and even he in the comments mentions that the setLocal is not there anymore and that you need to pass the context. 它与RC1有关,甚至他在评论中提到setLocal不再存在,你需要传递上下文。

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

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