2017-02-17 46 views
-1

嗨我是angular 2的新手,我在过去的一周中使用了angular 2,并想知道是否可以使用动态模板和动态样式生成动态组件。这意味着follwoing应该是这样的在Angular 2中创建一个动态组件

@Component({ 
    // 2a 
    selector: 'dynamic placeholder', 

    // 2b 
    styles: [`dynamic styles`] 

    // 2c 
    template: `dynmic template` 
}) 

是有可能做到这一点在角2,我记得这是这样,也许可能在角1

任何帮助将不胜感激(Escpecially plunkers用简单的代码)

这是我迄今取得:尝试使用ComponentFactoryResolver:

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

@Component({ 
    selector: 'my-app', 
    template: ` 
     <div>Hello, world!</div> 
    ` 
}) 
export class AppComponent { 
} 

@NgModule({ 
    declarations: [HomeComponent], 
    exports: [HomeComponent] 
}) 
export class HomeModule { 
} 

@Component({ 
    selector: 'home', 
    template: ` 
     <div>This is home</div> 
    ` 
}) 
export class HomeComponent { 
} 

@Component({ 
    selector: 'hello-world', 
    template: ` 
     <div> 
      Hello, world!, {{name}} 
      The answer is: {{getAnswer()}} 
     </div> 
    ` 
}) 

export class HelloWorldComponent implements AfterViewInit { 
    private name:string = 'You'; 

    constructor(private helloWorldService: HelloWorldService) { 
    } 

    ngAfterViewInit(): void { 
     this.name = 'Me'; 
    } 

    private getAnswer() { 
     return this.helloWorldService.giveMeTheAnswer(); 
    } 
} 

@NgModule({ 
    declarations: [HomeComponent, HelloWorldComponent], 
    providers: [HelloWorldService], 
    exports: [HomeComponent] 
}) 
export class HomeModule { 
} 

@Component({ 
    selector: 'home', 
    template: ` 
     <button (click)="sayHello()">Say hello</button> 
     <div>This is home</div> 
    ` 
}) 

export class HomeComponent { 
    constructor(private componentFactoryResolver: ComponentFactoryResolver, 
       private viewContainerRef: ViewContainerRef) { 
    } 

    private sayHello() { 
     const factory = this.componentFactoryResolver.resolveComponentFactory(HelloWorldComponent); 
     const ref = this.viewContainerRef.createComponent(factory); 
     ref.changeDetectorRef.detectChanges(); 
    } 
} 

这里是一个plunker使创建动态组件,我不知道是否创建动态CSS是可能的,我会很高兴,如果一些我可以回答我的问题: http://plnkr.co/edit/ZXsIWykqKZi5r75VMtw2?p=preview

+2

StackOverflow不是让你让人为你写代码的地方。向我们展示您的尝试并做出坚实的努力 –

+0

好的我会以正确的方式上传它,代码甚至不会工作 – Brk

+1

而不是试图关闭此线程,为什么不能互相帮助。我正在努力试图了解这个主题是否可能在角度2中。如果不是,我不会迁移到角度2或使用它。 我在互联网上传递了我的例子,对于我的案例没有确切的答案。 – Brk

回答

1

随着打字稿及最新版本Angular2的(我认为,功能已在2.4发布.0)可以创建1个基本组件,然后对其进行扩展。所有装饰/物业注释(@Input/@Output/@ViewChild)将被复制。但是,您必须为每个祖先@Component属性指定属性,即不能仅覆盖selector,而是覆盖所有内容。这是正确的做法。

另一种方法 - >使用reflect-metadata来更新组件类上的装饰器(可能是你正在寻找的,因为它可以在时间上覆盖1个属性),但要小心export(即公开)全部在您想要覆盖的组件内部使用的组件/指令/服务。例如,一些库有多个组件,其中一些只能在内部使用(即无法以正常方式将其导入到模块中;但是,您可以尝试providers)。如果您尝试“覆盖”,比如说,使用reflect-metadata的css,并且它使用内部组件 - > angular/typescript将崩溃,因为它无法解析“内部”内容。你可以从这个答案开始:StackOverflow

+0

谢谢蒂姆为快速回答,但是看起来在我看来附加答案,反映metdata是为继承而不是创建动态组件。 – Brk

+0

我给你解释的权​​利,但我正在寻找清楚如何使用反射元数据这样的工具的例子 – Brk

+0

好吧,可能我误解了你的问题。我以为你想分享组件之间的通用功能,而不是实时编码)。 –

相关问题