ng-content select 绑定变量

ng-content select bound variable

我正在尝试使用 angular2 创建表单生成器。一个非常基本的示例如下:

this.fields = [{name: 'Name', type: 'text'}, {name: 'Age', type: 'number'}];

但我也想支持自定义元素,例如:

this.fields = [
  {name: 'Name', type: text}, 
  {name: 'Age', type: 'custom', customid: 'Ctl1'},
  {name: 'Whatever', type: 'custom', customid: 'Ctl2'}
];
// template:
<super-form [fields]="fields">
  <Ctl1><input type="number" ...><Ctl1>
  <Ctl2><whaterver-control ...><Ctl2>
</super-form>

在我的表单构建器组件中,我有类似的东西:

<div *ngFor="let f of fields">
  <div [ngSwitch]="f.type">
    <span *ngSwitchWhen="'custom'">          
      <ng-content select="f.customid"></ng-content>
    </span>
  </div>
</div>

但考虑到我在这里,这显然行不通。这是 ng2 的限制吗?如果是这样,我想我可以硬编码说出 5 个可选内容元素并检查它们是否已指定并且没有动态选择,但这是一个 hack。

干杯

由于您要绑定到变量,请使用单向绑定语法,例如:

<ng-content [select]="f.customid"></ng-content>

更正
ng-content 仅用于静态投影。它意味着速度快 "transcluding"。请检查 this issue 了解更多信息

如果您用 <template> 元素包装内容,您已经可以做到这一点。

// renders the template
// `item` is just an example how to bind properties of the host component to the content passed as  template
@Directive({
  selector: '[templateWrapper]'
})
export class TemplateWrapper implements OnChanges {

  private embeddedViewRef:EmbeddedViewRef<any>;

  @Input()
  private item:any;

  constructor(private viewContainer:ViewContainerRef) {
    console.log('TemplateWrapper');
  }

  @Input() templateWrapper:TemplateRef<any>;

  ngOnChanges(changes:{[key:string]:SimpleChange}) {
    if (changes['templateWrapper']) {
      if (this.embeddedViewRef) {
        this.embeddedViewRef.destroy();
      }
      console.log('changes', changes);
      this.embeddedViewRef = this.viewContainer.createEmbeddedView(this.templateWrapper, {item: this.item});
    }

    if (this.embeddedViewRef) {
      console.log('context', this.embeddedViewRef.context);
      this.embeddedViewRef.context.item = this.item;
    }
  }
}
// just some component that is used in the passed template
@Component({
  selector: 'test-component',
  styles: [':host { display: block; border: solid 2px red;}'],
  directives: [TemplateWrapper],
  template: `
<div>test-comp</div>
<div>prop: {{prop | json}}</div>
  `
})
export class TestComponent {
  @Input() prop;

  constructor() {
    console.log('TestComponent');
  }
}
// the component the `<template>` is passed to to render it
@Component({
  selector: 'some-comp',
  directives: [TemplateWrapper],
  template: `
<div>some-comp</div>
<div *ngFor="let f of fields">
  <div [ngSwitch]="f.type">
    <span *ngSwitchCase="'custom'">         
      <template [templateWrapper]="template" [item]="f" ></template>
    </span>
  </div>
</div>  
  `
})
export class SomeComponent {

  constructor() {
    console.log('SomeComponent');
  }

  @ContentChild(TemplateRef) template;

  fields = [
    {name: 'a', type: 'custom'},
    {name: 'b', type: 'other'},
    {name: 'c', type: 'custom'}];
}
// the component where the `<template>` is passed to another component
@Component({
  selector: 'my-app',
  directives: [SomeComponent, TestComponent],
  template: `
<some-comp>
  <template let-item="item">
    <div>some content</div>
    <div>item: {{item | json}}</div>
    <test-component [prop]="item"></test-component>
  </template>
</some-comp>
  `,
})
export class App {
  constructor() {
    console.log('AppComponent');
  }
}

Plunker example

我知道这是一个老问题,但这是我在搜索此功能时首先到达的地方之一,所以我将添加我是如何解决它的。

ngContent 仅用于静态投影,因此您不能使用它进行任何绑定。如果您需要在投影内容中进行绑定,您可以使用 ngTemplateOutlet 和 ngOutletContext。

用法示例:

<my-component>
    <template let-item="item">
        <h1>{{item?.label}}</h1> - <span>{{item?.id}}</span>
    </template>
</my-component>

在 MyComponent 中,您可以使用 ContentChild 访问该模板:

@ContentChild(TemplateRef) templateVariable: TemplateRef<any>;

然后在组件的模板中将其传递给 ngTemplateOutlet,如下所示:

<div *ngFor="let item of list">
    <template [ngTemplateOutlet]="templateVariable" [ngOutletContext]="{item: item}"></template>
</div>

ngOutletContext 是可选的,但它允许您创建将在模板中绑定到的对象。请注意,我在上下文对象中创建了一个 属性 item。这与我在此处放置在模板上的名称相匹配:let-item="item"

现在 my-component 的消费者可以传入用于列表中每个项目的模板。

信用: 这 引导我走向正确的方向。