Angular2 Forms:作为输入字段 element.value 的结果在控制台获取 "Object"
Angular2 Forms: Getting "Object" at console as result of input field element.value
我试图以 angular2 的形式打印输入字段的内容,但结果是在控制台获取对象(我的 ControlGroup 类型),这是我的代码,为什么会发生这种情况
import { FORM_DIRECTIVES, ControlGroup, AbstractControl,
FormBuilder, Validators} from 'angular2/common';
@Component({
selector : 'formbuilder',
template : `
<h1> FOrm Using Formbuilder </h1>
<form [ngFormModel] = "myform" (ngSubmit) = "onSubmit(myform.value)">
<lable>Name: </lable>
<input type = "text" [ngFormControl] = "myform.controls['input1']" />
<input type = "text" [ngFormControl] = "myform.controls['input2']" />
<button type = "text">Submit</button>
</form>
`,
directives : [FORM_DIRECTIVES]
})
export class formbuilder {
myform : ControlGroup ;
ac : AbstractControl;
constructor(fb : FormBuilder) {
this.myform = fb.group({
'input1' : ['ABC', Validators.required],
'input2' : ['DEF', Validators.required]
});
this.ac = this.myform.controls['ac']
}
onSubmit(value : any) : void {
console.log("submit method called " + value);
}
}
bootstrap(formbuilder);
我刚刚测试了您的代码,myForm.value
包含以下内容:
{input1: "ABC", input2: "DEF"}
这对应于您定义的所有控件的值。
如果你想输入一个特定的字段,你可以使用这个:
this.myform.controls.input1.value;
我试图以 angular2 的形式打印输入字段的内容,但结果是在控制台获取对象(我的 ControlGroup 类型),这是我的代码,为什么会发生这种情况
import { FORM_DIRECTIVES, ControlGroup, AbstractControl,
FormBuilder, Validators} from 'angular2/common';
@Component({
selector : 'formbuilder',
template : `
<h1> FOrm Using Formbuilder </h1>
<form [ngFormModel] = "myform" (ngSubmit) = "onSubmit(myform.value)">
<lable>Name: </lable>
<input type = "text" [ngFormControl] = "myform.controls['input1']" />
<input type = "text" [ngFormControl] = "myform.controls['input2']" />
<button type = "text">Submit</button>
</form>
`,
directives : [FORM_DIRECTIVES]
})
export class formbuilder {
myform : ControlGroup ;
ac : AbstractControl;
constructor(fb : FormBuilder) {
this.myform = fb.group({
'input1' : ['ABC', Validators.required],
'input2' : ['DEF', Validators.required]
});
this.ac = this.myform.controls['ac']
}
onSubmit(value : any) : void {
console.log("submit method called " + value);
}
}
bootstrap(formbuilder);
我刚刚测试了您的代码,myForm.value
包含以下内容:
{input1: "ABC", input2: "DEF"}
这对应于您定义的所有控件的值。
如果你想输入一个特定的字段,你可以使用这个:
this.myform.controls.input1.value;