angular 2 error:无法解析FormGroup的所有参数

时间:2016-11-10 21:51:42

标签: javascript forms angular

我正在关注关于FormBuilder http://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html的Whiletram的教程我复制了代码并更改了一些变量名。我正在使用Angular 2.1.2,Typescript 2.0.8和Angular Material2(来自Google)。 Atom Typescript表示任何页面都没有错误。尽管如此,我在加载时遇到错误,并且页面没有加载这个新代码。

zone.js:388 Unhandled Promise rejection: Can't resolve all parameters for FormGroup: (?, ?, ?). ; Zone: <root> ; Task: Promise.then ; Value: Error: Can't resolve all parameters for FormGroup: (?, ?, ?).(…) Error: Can't resolve all parameters for FormGroup: (?, ?, ?).
at CompileMetadataResolver.getDependenciesMetadata (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:14598:21)
at CompileMetadataResolver.getTypeMetadata (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:14499:28)
at eval (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:14642:43)
at Array.forEach (native)
at CompileMetadataResolver.getProvidersMetadata (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:14622:21)
at CompileMetadataResolver.getDirectiveMetadata (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:14262:36)
at eval (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:14367:51)
at Array.forEach (native)
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:14361:51)
at RuntimeCompiler._compileComponents (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:17063:49)consoleError @ zone.js:388_loop_1 @ zone.js:417drainMicroTaskQueue @ zone.js:421ZoneTask.invoke @ zone.js:339
2016-11-10 16:01:40.394 zone.js:390 Error: Uncaught (in promise): Error: Can't resolve all parameters for FormGroup: (?, ?, ?).(…)consoleError @ zone.js:390_loop_1 @ zone.js:417drainMicroTaskQueue @ zone.js:421ZoneTask.invoke @ zone.js:339
apartment4Rent.component.html

HTML示例

<form [formGroup]="registerApartment4RentForm" (submit)="onSubmit($event)">
  <md-input id="country" name="country" class="required" aria-labelledby="country" formControlName="country"
i18n-placeholder="select country placeholder" placeholder="Country" type="text"  size="30" maxlength="30">
  </md-input>

加上4个相同的州,城市,街道和邮政编码

AppComponent ,包含教程中的构造函数 - 在app文件夹中

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';

@Component({
    moduleId: module.id,
    selector: 'residence-app',
    templateUrl: "components/navigation/headerFooter.html",
    styleUrls: [ "components/navigation/styleHeaderFooter.css" ],
    providers: [
      FormBuilder,
      FormGroup
    ]
})
export class AppComponent implements OnInit {
    registerApartment4RentForm: FormGroup;
    constructor(private formBuilder: FormBuilder) {}
    ngOnInit() {
      this.registerApartment4RentForm = this.formBuilder.group({
        country: '',
        stateProvince: '',
        address: this.formBuilder.group({
          streetAddress: '',
          zipCode: '',
          city: ''
        })
      });
    }
}

templateUrl中的headerFooter.html有一些标题和页脚的html和

            <div>
                <router-outlet></router-outlet>
            </div>
在我尝试使用此反应式表单代码之前,在loading4Rent.component.html加载和显示之间的

。我的目录结构是app / components / input / apartment4Rent.component(s)

要使反应形式发挥作用需要改变什么?

2 个答案:

答案 0 :(得分:5)

您是否将Angular反应表单模块添加为模块的导入?它看起来像是缺少该模块注册的服务。

(我在手机上,很抱歉这个简短的回答)

修改

这就是我的意思:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@anglar/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  imports: [BrowserModule, ReactiveFormsModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}

请注意ReactiveFormsModule的导入

答案 1 :(得分:0)

此错误的另一个原因是您两次导入了表单组。我在应用模块中将其删除,现在对我有用,希望它对某人有所帮助。