我'm trying to implement Dynamic Forms in Angular 2. I'已向动态表单添加了删除和取消等附加功能 . 我已经按照这个文档:https://angular.io/docs/ts/latest/cookbook/dynamic-form.html
我对代码做了一些更改 . 我在这里收到错误 .
我怎么做这个错误去?
你可以在这里找到完整的代码:http://plnkr.co/edit/SL949g1hQQrnRUr1XXqt?p=preview,它在plunker中工作,但不在我的本地系统中 .
Html代码:
<div>
<form [formGroup]="form">
<div *ngFor="let question of questions" class="form-row">
<label [attr.for]="question.key">{{question.label}}</label>
<div [ngSwitch]="question.controlType">
<input *ngSwitchCase="'textbox'" [formControlName]="question.key"
[id]="question.key" [type]="question.type" [(ngModel)]="question.value">
<select [id]="question.key" [(ngModel)]="question.value" *ngSwitchCase="'dropdown'" [formControlName]="question.key" >
<option *ngFor="let opt of question.options" [ngValue]="opt.key" >{{opt.value}}</option>
</select>
<input *ngSwitchCase="'checkbox'" [(ngModel)]="question.value"
[id]="question.key" [type]="question.type" (change)="question.value = ck.checked" #ck [ngModelOptions]="{standalone: true}">
</div>
<div class="errorMessage" *ngIf="!form.controls[question.key].valid">{{question.label}} is required</div>
</div>
<div class="form-row">
<button type="submit" [disabled]="!form.valid" (click)="onSubmit()">Save</button>
<button type="button" class="btn btn-default" (click)="cancel()">Cancel</button>
<button type="button" class="btn btn-default" (click)="clear()">Clear</button>
</div>
</form>
<div *ngIf="payLoad" class="form-row">
<strong>Saved the following values</strong><br>{{payLoad}}
</div>
</div>
组件代码:
import { Component, Input, OnInit } from '@angular/core';
import { FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { QuestionBase } from './question-base';
import { QuestionControlService } from './question-control.service';
import { ControlGroup } from '@angular/common';
import {ChangeDetectorRef} from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'dynamic-form',
templateUrl: 'app/dynamicform/form.component.html',
directives: [REACTIVE_FORM_DIRECTIVES],
providers: [QuestionControlService]
})
export class DynamicFormComponent implements OnInit {
@Input() questions: QuestionBase<any>[] = [];
form: FormGroup;
payLoad:any;
payLoad2:any;
questiont: QuestionBase<any>;
questiond: QuestionBase<any>;
constructor(private qcs: QuestionControlService, private cdr: ChangeDetectorRef) { }
ngOnInit() {
this.form = this.qcs.toFormGroup(this.questions);
console.log("Form Init",this.questions);
this.questiont = JSON.parse(JSON.stringify(this.questions));
this.questiond = JSON.parse(JSON.stringify(this.questions));
}
onSubmit() {
this.payLoad = JSON.stringify(this.form.value);
this.payLoad2=this.payLoad;
this.questiont = JSON.parse(JSON.stringify(this.questions));
console.log("Submitted data",this.questions);
}
cancel(){
console.log("Canceled");
this.questions = JSON.parse(JSON.stringify(this.questiont));
}
clear(){
this.questions = JSON.parse(JSON.stringify(this.questiond));
this.questiont = JSON.parse(JSON.stringify(this.questiond));
console.log("Cleared");
this.cdr.detectChanges();
}
}
12 回答
如果您使用Karma,这个答案可能对您有所帮助:
我完全按照@ wmnitin的答案提到的那样做,但错误始终存在 . 当使用“ng serve”而不是“karma start”时,它可以工作!
为了在使用AppModule(NgModule)时使ngModel工作,您必须在AppModule中导入FormsModule .
像这样:
升级到RC5后我遇到类似的错误;即Angular 2:无法绑定到'ngModel',因为它不是'input'的已知属性 .
Plunker上的代码显示您使用Angular2 RC4,但https://angular.io/docs/ts/latest/cookbook/dynamic-form.html的示例代码使用的是NGModule,它是RC5的一部分 . NGModules是从RC4到RC5的重大变化 .
此页面介绍了从RC4到RC5的迁移:https://angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html
我希望这可以解决您所遇到的错误,并帮助您朝着正确的方向前进 .
简而言之,我必须在app.module.ts中创建一个NGModule:
然后我改变main.ts来使用模块:
当然,我还需要更新package.json中的依赖项 . 这是我从package.json的依赖项 . 不可否认,我已经从其他来源(也许是ng docs示例)中将它们混在一起,因此您的里程可能会有所不同:
我希望这会有所帮助 . :-)
步骤跟随
1.打开
app.module.ts
文件 ..
2.添加
import { FormsModule } from '@angular/forms';
.
3.将
FormsModule
添加到imports
作为imports: [ BrowserModule, FormsModule ],
.
最终结果将如下所示
首先从angular lib和NgModule导入FormsModule在导入中声明它
您需要将@ angular / forms依赖项导入模块 .
如果您使用的是npm,请安装依赖项:
将其导入您的模块:
如果您使用SystemJs加载模块
现在您可以使用[(ngModel)]两种方式进行数据绑定 .
您需要在@NgModule装饰器中导入FormsModule,而@NgModule存在于moduleName.module.ts文件中 .
您还应该添加缺少的那些 .
为了能够使用
'ngModule'
,需要将'FormsModule'
(来自@angular/forms
)添加到AppModule
中的import[]
数组中(默认情况下应该在CLI项目中) .在Googling中找到此解决方案,更新您的@NgModule代码,如下所示:
资料来源:Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’
您只需添加
FormsModule
并在app.module.ts
文件中导入FormsModule
.只需在
app.module.ts
中添加以上两行即可 . 它工作正常 .由于Angular 6中的某些原因,只是导入FormsModule并没有解决我的问题 . 最后解决我的问题的是添加