我在使用带有表单的 ngFormModel 指令时遇到了一些问题,它说“TypeError: Cannot read property 'validator' of undefined in form_one (我从 NG-BOOK 2 Tutorial 编写了这段代码,但它不起作用!)
import {Component} from 'angular2/core'
import {
FORM_DIRECTIVES,
FormBuilder,
ControlGroup,
Validators
} from 'angular2/common'
@Component({
selector: 'demo-form-sku-builder',
directives: [FORM_DIRECTIVES],
template: `
<div class="ui raised segment">
<h2 class="ui header">Demo Form: Sku with FormBuilder</h2>
<form class="ui form"
[ngFormModel]="myForm"
(ngSubmit)="onSubmit(myForm.value)">
<div class="field">
<label for="skuInput">SKU</label>
<input type="text"
id="skuInput"
placeholder="SKU"
[ngFormControl]="myForm.controls['sku']">
</div>
<button type="submit" class="ui button">Submit</button>
</form>
</div>
`
})
export class DemoFormSkuBuilder {
contructor (formBuilder) {
this.myForm = formBuilder.group({
'sku': ['', Validators.required]
})
}
onSubmit(value) {
console.log("you submitted value: ", value);
}
}
编辑:(@Thierry Templier)
contructor (formBuilder) {
this.fb = formBuilder;
this.myForm = this.fb.group({
'sku': ['', Validators.required]
})
}
static get parameters() {
return [[FormBuilder]];
}
我的图书馆:
import 'es6-shim';
import 'reflect-metadata';
import 'zone.js/dist/zone-microtask'
import 'rxjs/Rx'
import 'es6-promise'