2

我无法让我的 child-formgroup 工作。找不到原因。我在控制台中看到的错误是:“formGroup 需要一个 FormGroup 实例。”

我的组件

    constructor(private _formBuilder:FormBuilder) {}

    ngOnInit() {
            this.form = this._formBuilder.group({
                email: ['', [Validators.required, ValidationService.emailValidator]],
                oldPassword: ['', [Validators.required, Validators.minLength(6)]],
                passwords: this._formBuilder.group({
                    newPassword: ['', [Validators.required, Validators.minLength(6)]],
                    confirmPassword: ['', [Validators.required, Validators.minLength(6)]],
                }, {validator: ValidationService.areEqual})

            });
    }

我的 HTML

<form [formGroup]="form">
        <div [formGroup]="passwords">
            <div class="form-group">
                <label>New password</label>
                <input formControlName="newPassword" placeholder="Password" type="password">
            </div>

            <div class="form-group">
                <label>Confirm password</label>
                <input formControlName="confirmPassword" placeholder="Confirm password" type="password">
            </div>
        </div>
</form>
4

1 回答 1

3

您的嵌套表单组应该是:

<div formGroupName="passwords">  

代替

<div [formGroup]="passwords">

这是一个

普朗克

于 2017-03-20T15:36:05.070 回答