我正在寻找使用这个example https://angular.io/docs/ts/latest/cookbook/dynamic-form.htmlAngular 文档中提供了与 [(ngmodel)] 结合使用动态表单的功能。以便动态表单控件可以绑定到模型。
但是,当我尝试将控件绑定到 [(ngMode)] 时,我看到错误消息
检查后表情发生了变化。先前值:“假”。当前值:“真”。
Here https://plnkr.co/edit/3hpld2nUTIVgzhHbscaY?p=preview是一个经过更改的 Plunker 示例。
我修改了 QuestionBase 类来保存密钥
export class QuestionBase<T>{
...
modelKey:string;
...
}
在 QuestionService 中,每个问题现在都有它应该更新的模型的名称
new TextboxQuestion({
...
modelKey: 'firstName'
}),
在动态表单问题组件中,模型作为输入变量传入
export class DynamicFormQuestionComponent {
...
@Input() model: SampleModel;
...
}
DynamicFormQuestionComponent HTML 字段已修改为使用 [(ngModel)]
<input *ngSwitchCase="'textbox'" [formControlName]="question.key"
[id]="question.key" [type]="question.type" [(ngModel)]="model[question.modelKey]">
这里的控件应该绑定到已解析的“model[question.modelKey]”
鉴于我没有看到官方文档中提供的示例中使用了 [(ngModel)] 。
如有任何帮助,我们将不胜感激。
非常感谢你。