简体   繁体   English

Angular2 ComponentResolver和ng-content不呈现

[英]Angular2 ComponentResolver and ng-content not rendering

I'm trying to use ComponentResolver and ng-content together, however, when the resolver builds the component the transcluded content is not rendered. 我正在尝试将ComponentResolverng-content一起使用,但是,当解析器构建组件时,不会呈现已转换的内容。

I'm using Angular2 RC3. 我正在使用Angular2 RC3。 I do know there is some upcoming changes coming to the ComponentResolver ( https://github.com/angular/angular/issues/9467 ) that might affect this. 我知道有一些即将发生的变化可能会影响到ComponentResolverhttps://github.com/angular/angular/issues/9467 )。

import {
  Input, 
  Component, 
  OnInit, 
  ViewContainerRef, 
  ComponentResolver, 
  ViewChild 
} from '@angular/core';
/**
 * COLUMN
 */
@Component({
    selector: 'column-component',
    template: `
      <div style="border:1px solid green; padding:5px;display:block;">
        <b>column: {{label}}</b>
        <ng-content></ng-content>
      </div>`
})
export class ColumnComponent {
  @Input() label: sting = "";
}
/**
 * CELL
 */
@Component({
    selector: 'cell-component',
    template: '<b>cell component</b> <div #target></div>'
})
export class CellComponent {

  @ViewChild('target', {read: ViewContainerRef}) target;

  constructor(
    private componentResolver:ComponentResolver, 
    private viewContainerRef: ViewContainerRef) {
  }

  ngOnInit() {
    this.componentResolver.resolveComponent(ColumnComponent).then((factory) => {
      let componentRef = this.target.createComponent(factory, 0, this.viewContainerRef.injector);
    });
  }
}
/**
 * TABLE
 */
@Component({
    selector: 'datatable',
    template: `
      <div #target style="border:1px solid blue;padding:5px;">
        <b>table component</b>
        <cell-component
           style="border:1px solid yellow;padding:5px;display:block">
         </cell-component>
      </div>
    `,
    directives: [ CellComponent ]
})
export class TableComponent {
}
/**
 * ROOOT APP
 */
@Component({
    selector: 'root-app',
    template: `
      <div style="border:1px solid red;padding:5px;">
        <datatable>
          <column-component [label]="'my label'">
            column content
          </column-component>
        </datatable>
      </div>
    `,
    directives: [ ColumnComponent, TableComponent ]
})
export class AppComponent {

}

Current behavior: The words 'column content' that are content of the column-component are never rendered. 当前行为:永远不会呈现作为column-component内容的单词“列内容”。

Expected behavior :The inner content ( words "column content" ) of the column-component would render. 预期行为column-component的内部内容(单词“列内容”)将呈现。


UPDATE UPDATE

@GünterZöchbauer answered my orginial question ( THANKS! ), however, I applied a bit more logic and sadly it did not work. @GünterZöchbauer回答了我的问题(谢谢!),然而,我应用了更多的逻辑,遗憾的是它没有用。 See updated plunkr: https://plnkr.co/edit/CsbmY6wePC3AWZlBDy34?p=preview 请参阅更新的plunkr: https ://plnkr.co/edit/CsbmY6wePC3AWZlBDy34 p = preview

Expected Results: The cells would be repeated for each row and the value of the row 's age attribute would be transposed to each. 预期结果:将对每一行重复单元格,并将rowage属性的值转换为每行。

You need to add <ng-content> to intermediate components as well 您还需要将<ng-content>添加到中间组件

Plunker example Plunker的例子

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

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