gat*_*pia 18 angular2-forms angular
我正在尝试使用角度2创建表单构建器.一个非常基本的示例如下:
this.fields = [{name: 'Name', type: 'text'}, {name: 'Age', type: 'number'}];
Run Code Online (Sandbox Code Playgroud)
但我也想支持自定义元素,如:
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>
Run Code Online (Sandbox Code Playgroud)
在我的表单构建器组件中,我有类似的东西:
<div *ngFor="let f of fields">
<div [ngSwitch]="f.type">
<span *ngSwitchWhen="'custom'">
<ng-content select="f.customid"></ng-content>
</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但鉴于我在这里,这显然不起作用.这是ng2限制吗?如果是这样,我想我可以硬代码说出5个可选的内容元素并检查它们是否被指定而没有动态选择,但这是一个黑客.
干杯
Jus*_*tin 30
我知道这是一个老问题,但这是我在搜索此功能时首先登陆的地方之一,因此我将添加我能够解决的问题.
ngContent仅用于静态投影,因此您无法使用它来执行任何绑定.如果您需要在预计内容中绑定,可以使用ngTemplateOutlet和ngOutletContext.
用法示例:
<my-component>
<template let-item="item">
<h1>{{item?.label}}</h1> - <span>{{item?.id}}</span>
</template>
</my-component>
Run Code Online (Sandbox Code Playgroud)
在MyComponent内部,您可以使用ContentChild访问该模板:
@ContentChild(TemplateRef) templateVariable: TemplateRef<any>;
Run Code Online (Sandbox Code Playgroud)
然后在组件的模板中,将其传递给ngTemplateOutlet,如下所示:
<div *ngFor="let item of list">
<template [ngTemplateOutlet]="templateVariable" [ngOutletContext]="{item: item}"></template>
</div>
Run Code Online (Sandbox Code Playgroud)
ngOutletContext是可选的,但它允许您创建将在模板中绑定到的对象.请注意,我item
在上下文对象中创建了一个属性.这匹配我在模板上放置的名称:let-item="item"
现在消费者my-component
可以传入模板用于列表中的每个项目.
信用:这个答案让我朝着正确的方向前进.
Gün*_*uer 10
如果使用<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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
// 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');
}
}
Run Code Online (Sandbox Code Playgroud)
// 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'}];
}
Run Code Online (Sandbox Code Playgroud)
// 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');
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15289 次 |
最近记录: |