带有子组件多个实例的Angular2父组件 [英] Angular2 Parent Component with multiple instances of child component

查看:152
本文介绍了带有子组件多个实例的Angular2父组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父组件,该组件的模板引用了同一子组件的多个实例.子组件将具有相同的组件/模板实现,但应实例化多次,并将呈现不同的数据集.我查看了很多帖子,例如实例Angular 2组件两次,但全部他们中的似乎使用了已弃用的指令component属性.

I have a parent component with a template that references multiple instances of the same child component. The child components would have the same component/template implementation but should be instantiated multiple times and would render different data sets. I looked into a lot of posts like Instance Angular 2 Component Two times but all of them seemed to use the deprecated directives component attribute.

parent.template.html

parent.template.html

<child-component data="foo"></child-component>
<child-component data="baz"></child-component>

data.service.ts

data.service.ts

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

@Injectable()
export class DataService {
    getFooData(): Object {
        return { name: 'Foo' }
    }

    getBazData(): Object {
        return { name: 'Baz' }
    }
 }

child.template.html

child.template.html

<h1>{{objectToRender.name}}</h1>

child.component.ts

child.component.ts

import { Component, Input, OnInit } from '@angular/core';
import { DataService }              from './data.service';

@Component({
    selector:       'child-component',
    templateUrl:    'child.template.html',
    providers:      [ DataService ]
})
export class ChildComponent  implements OnInit {
    @Input() data: String;
    objectToRender: Object;
    ds:             DataService

    constructor( private dataService: DataService ) {
        this.ds = dataService;
    }

    ngOnInit(){
        switch( this.data ) {
            case 'foo':
                this.objectToRender = this.ds.getFooData();
            case 'baz':
                this.objectToRender = this.ds.getBazData();
        }
    }

}    

app.module.ts

app.module.ts

import { NgModule }             from '@angular/core';
import { BrowserModule }        from '@angular/platform-browser';
import { AppComponent }         from './app.component';
import { ParentComponent}     from './parent.component';
import { ChildComponent }     from './child.component';


@NgModule({
    imports:      [ BrowserModule ],
    declarations: [ AppComponent, ParentComponent, ChildComponent ],
    bootstrap:    [ AppComponent ]
})
export class AppModule { }

结果: 巴兹 巴兹

预期: oo 巴兹

在这个Plunker中甚至可以看到我的问题的简化版本.我已经一起省略了模板文件,并使用根应用程序组件作为父级. https://plnkr.co/edit/QI5lGH3S9a5o3b1ajPBl?p=preview

An even simplified version of my problem can be seen in this Plunker. I've omitted template files all together and use the root app component as a parent. https://plnkr.co/edit/QI5lGH3S9a5o3b1ajPBl?p=preview

非常感谢!

推荐答案

这是缺少的break;

    switch( this.data ) {
        case 'foo':
            this.objectToRender = this.ds.getFooData();
            break;
        case 'baz':
            this.objectToRender = this.ds.getBazData();
            break;
    }

柱塞示例

这篇关于带有子组件多个实例的Angular2父组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆