- 首页
- 其他开发
- formControlName 和 FormControl 有什么区别?
formControlName 和 FormControl 有什么区别?
[英] What is the difference between formControlName and FormControl?
本文介绍了formControlName 和 FormControl 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Angular2 的 ReactiveFormsModule
来创建一个包含表单的组件.这是我的代码:
foo.component.ts:
constructor(fb: FormBuilder) {this.myForm = fb.group({'全名': ['', Validators.required],'性别': []});}
foo.component.html(带有 [formControl]
):
<div class="field"><label>全名*</label><input type="text" [formControl]="myForm.controls.fullname"/>
<div class="内联字段"><label for="gender">性别</label><div class="field"><div class="ui radio checkbox"><input type="radio" name="gender" checked="" tabindex="0" class="hidden" [formControl]="myForm.controls.gender"><label>男</label>
<div class="field"><div class="ui radio checkbox"><input type="radio" name="gender" tabindex="0" class="hidden" [formControl]="myForm.controls.gender"><label>女性</label>
foo.component.html(带有 formControlName
):
<div class="field"><label>全名*</label><input type="text" formControlName="fullname"/>
<div class="内联字段"><label for="gender">性别</label><div class="field"><div class="ui radio checkbox"><input type="radio" name="gender" checked="" tabindex="0" class="hidden" formControlName="gender"><label>男</label>
<div class="field"><div class="ui radio checkbox"><input type="radio" name="gender" tabindex="0" class="hidden" formControlName="gender"><label>女性</label>
两种方式都可以.但我无法弄清楚使用 [formControl]
和 formControlName
之间有什么区别.
解决方案
我相信你错过了一个重要的点:第二个例子中的 [formGroup]
指令.formControlName
与 [formGroup]
一起使用来保存表单的多点导航.例如:
<input type="text" [formControl]="myForm.controls.firstName"/><input type="text" [formControl]="myForm.controls.lastName"/><input type="text" [formControl]="myForm.controls.email"/><input type="text" [formControl]="myForm.controls.title"/>
相当于:
<input type="text" formControlName="firstName"/><input type="text" formControlName="lastName"/><input type="text" formControlName="email"/><input type="text" formControlName="title"/>
现在想象嵌套的 FormGroups
:)
I'm using ReactiveFormsModule
of Angular2 to create a component that contains a form. Here is my code:
foo.component.ts:
constructor(fb: FormBuilder) {
this.myForm = fb.group({
'fullname': ['', Validators.required],
'gender': []
});
}
foo.component.html (with [formControl]
):
<div class="fields">
<div class="field">
<label>Fullname*</label>
<input type="text" [formControl]="myForm.controls.fullname"/>
</div>
</div>
<div class="inline fields">
<label for="gender">Gender</label>
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="gender" checked="" tabindex="0" class="hidden" [formControl]="myForm.controls.gender">
<label>Male</label>
</div>
</div>
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="gender" tabindex="0" class="hidden" [formControl]="myForm.controls.gender">
<label>Female</label>
</div>
</div>
</div>
foo.component.html (with formControlName
):
<div class="fields">
<div class="field">
<label>Fullname*</label>
<input type="text" formControlName="fullname"/>
</div>
</div>
<div class="inline fields">
<label for="gender">Gender</label>
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="gender" checked="" tabindex="0" class="hidden" formControlName="gender">
<label>Male</label>
</div>
</div>
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="gender" tabindex="0" class="hidden" formControlName="gender">
<label>Female</label>
</div>
</div>
</div>
Both ways work. But i cannot figure out what is the difference between using [formControl]
and formControlName
.
解决方案
I believe you missed an important point: [formGroup]
directive in the second example. formControlName
is used together with [formGroup]
to save your form multiple dot navigations. For example:
<div>
<input type="text" [formControl]="myForm.controls.firstName"/>
<input type="text" [formControl]="myForm.controls.lastName"/>
<input type="text" [formControl]="myForm.controls.email"/>
<input type="text" [formControl]="myForm.controls.title"/>
</div>
Is equivalent to:
<div [formGroup]="myForm">
<input type="text" formControlName="firstName"/>
<input type="text" formControlName="lastName"/>
<input type="text" formControlName="email"/>
<input type="text" formControlName="title"/>
</div>
Now imagine nested FormGroups
:)
这篇关于formControlName 和 FormControl 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文