formControlName和FormControl有什么区别?

时间:2016-10-21 08:33:05

标签: angular radio-button angular2-forms form-control angular2-formbuilder

我正在使用Angular2的ReactiveFormsModule来创建包含表单的组件。这是我的代码:

foo.component.ts

constructor(fb: FormBuilder) {
    this.myForm = fb.group({
        'fullname': ['', Validators.required],
        'gender': []
    });
}

foo.component.html (使用[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 (使用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>

两种方式都有效。但我无法弄清楚使用[formControl]formControlName之间的区别。

5 个答案:

答案 0 :(得分:111)

我相信你错过了一个重点:第二个例子中的[formGroup]指令。 formControlName[formGroup]一起用于保存表单多点导航。例如:

<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>

相当于:

<div [formGroup]="myForm">
  <input type="text" formControlName="firstName"/>
  <input type="text" formControlName="lastName"/>
  <input type="text" formControlName="email"/>
  <input type="text" formControlName="title"/>
</div>

现在想象一下嵌套的FormGroups:)

答案 1 :(得分:16)

[formControl]将您创建的FormControl个实例的引用分配给FormControlDirective

formControlName为表单模块分配一个字符串,以按名称查找控件。

如果您为控件创建变量,您也不需要Harry提到的.,但我也建议使用[formGroup],因为更复杂的形式可能会变成凌乱。

constructor(fb: FormBuilder) {
    this.fullName = new FormControl('', Validators.required);
    this.gender = new FormControl('');
    this.myForm = fb.group({
        'fullname': this.fullName,
        'gender': this.gender
    });
}

答案 2 :(得分:4)

在接受的答案中提供的第二个等效性,这是(不推荐):

<div [formGroup]="myForm">
  <input type="text" [formControl]="firstName"/>
  <input type="text" [formControl]="lastName"/>
  <input type="text" [formControl]="email"/>
  <input type="text" [formControl]="title"/>
</div>

请注意,我们仍在使用[formGroup]指令。

但是,要使此模板无错误地编译,那么组件需要将控件声明为AbstractControls而不是FormControls:

myComponent.ts

firstName: AbstractControl
lastName: AbstractControl
email: AbstractControl
title: AbstractControl

但是,请注意,声明AbstractControls是not recommended,因此如果您收到错误Cannot find control with unspecified name attribute,则很可能混合了样式或将控件声明为AbstractControls。

答案 3 :(得分:1)

从Angular文档(https://angular.io/guide/reactive-forms):

组件

@Component({
  ...
})
export class ProfileEditorComponent {
  profileForm = new FormGroup({
    firstName: new FormControl(''),
    lastName: new FormControl(''),
  });
}

模板

<form [formGroup]="profileForm">

  <label>
    First Name:
    <input type="text" formControlName="firstName">
  </label>

  <label>
    Last Name:
    <input type="text" formControlName="lastName">
  </label>

</form>
  

请注意,就像FormGroup包含一组控件一样,   profileForm FormGroup使用FormGroup绑定到form元素   指令,在模型与   包含输入的表格。 formControlName输入由   FormControlName指令将每个单独的输入绑定到表单   FormGroup

中定义的控件

答案 4 :(得分:0)

使用[formControl],您可以使用反应性编程优势,因为FormControl有一个名为valueChanges的属性(我现在知道这个,可能还有更多)返回{{ 1}}您可以订阅并使用它。 (例如,在用户更改值时,您希望检查输入电子邮件不会重复的注册方案非常有用)