我已经创建了一些定制组件,其中的每一个实现ControlValueAccessor
,以便它们可以作为FormGroup
内控制功能内注册一个动态添加定制组件的一种形式的控制。通常,唯一要做的事情就是将formControlName
指令添加到HTML中的每个自定义组件。如何将formgroup
不过,我在运行时使用this技术附加这些部件的形式。
我的问题是,我不能注册这些组件与包含FormGroup
因为我不能声明formControlName
指令(或任何指令,对于这一点)与动态添加控制。
有其他人发现了如何这可能是可能的吗?目前,我正在包装的另一个组件内的每个控制,这样我就可以使用formControlName
指令,但是这仅仅是太丑陋和劳动密集型的。
下面是什么,我已经作为一个独立的应用程序Angular2,这说明了一个组件(CustomComponent
)实现在启动时被编程添加一个精简的例子。为了绑定CustomComponent
到FormGroup
,我已经创造CustomContainerComponent
我宁愿避免。
import {
Component, ReflectiveInjector, ViewContainerRef, Compiler, NgModule, ModuleWithComponentFactories,
OnInit, ViewChild, forwardRef
} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {NG_VALUE_ACCESSOR, ControlValueAccessor, FormGroup, FormBuilder, ReactiveFormsModule} from '@angular/forms';
export class AbstractValueAccessor<T> implements ControlValueAccessor {
_value: T;
get value(): T {
return this._value;
};
set value(v: T) {
if (v !== this._value) {
this._value = v;
this.onChange(v);
}
}
writeValue(value: T) {
this._value = value;
this.onChange(value);
}
onChange = (_) => {};
onTouched =() => {};
registerOnChange(fn: (_: any) => void): void {
this.onChange = fn;
}
registerOnTouched(fn:() => void): void {
this.onTouched = fn;
}
}
export function MakeProvider(type: any) {
return {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => type),
multi: true
};
}
@Component({
selector: 'app-custom',
template: `<input type="text" [value]="value">`,
providers: [MakeProvider(CustomComponent)]
})
class CustomComponent extends AbstractValueAccessor<string> {
}
@Component({
selector: 'app-custom-container',
template: `<div [formGroup]="formGroup"><app-custom formControlName="comp"></app-custom></div>`
})
class CustomContainerComponent {
formGroup: FormGroup;
}
@NgModule({
imports: [BrowserModule, ReactiveFormsModule],
declarations: [CustomComponent, CustomContainerComponent]
})
class DynamicModule {
}
@Component({
selector: 'app-root',
template: `<h4>Dynamic Components</h4><br>
<form [formGroup]="formGroup">
<div #dynamicContentPlaceholder></div>
</form>`
})
export class AppComponent implements OnInit {
@ViewChild('dynamicContentPlaceholder', {read: ViewContainerRef})
public readonly vcRef: ViewContainerRef;
factory: ModuleWithComponentFactories<DynamicModule>;
formGroup: FormGroup;
constructor(private compiler: Compiler, private formBuilder: FormBuilder) {
}
ngOnInit() {
this.compiler.compileModuleAndAllComponentsAsync(DynamicModule)
.then((moduleWithComponentFactories: ModuleWithComponentFactories<DynamicModule>) => {
this.factory = moduleWithComponentFactories;
const compFactory = this.factory.componentFactories.find(x => x.selector === 'app-custom-container');
const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
let cmp = this.vcRef.createComponent(compFactory, this.vcRef.length, injector, []);
(<CustomContainerComponent>cmp.instance).formGroup = this.formGroup;
});
this.formGroup = this.formBuilder.group({
'comp': ['hello world']
})
}
}
@NgModule({
imports: [BrowserModule, ReactiveFormsModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {
}
无代码无哭...添加你在这里尝试 – smnbbrv
你是非常正确的。我会添加一些代码 –
感谢您的更新。动态创建组件的最终用途是什么?你想只有动态控件的动态表单,或者你想有一个一个地使用控件的能力吗? – smnbbrv