简体   繁体   中英

angular 2 error: Can't resolve all parameters for FormGroup

I am following Thoughtram's tutorial for FormBuilder http://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html I copied the code and changed a few variable names. I'm using Angular 2.1.2, Typescript 2.0.8, and Angular Material2 (from Google). Atom Typescript says no error on any page. Nonetheless, I get errors at load, and the page does not load with this new code.

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

HTML sample for apartment4Rent.component.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>

plus 4 more identically for state, city, street, and ZIPcode

AppComponent with constructor from the tutorial - in the app folder

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: ''
        })
      });
    }
}

The headerFooter.html in the templateUrl has some html for a header and a footer and

            <div>
                <router-outlet></router-outlet>
            </div>

in between where apartment4Rent.component.html loads and appears before I tried this reactive form code. My directory structure is app/components/input/apartment4Rent.component(s)

What has to change to make reactive forms work?

Did you add the Angular reactive forms module as an import to your module? It looks like it's missing the services that are registered by that module.

(am on my phone, so sorry for the brief answer)

Edit

This is what I mean:

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

Note the import of ReactiveFormsModule

One more reason for this error is when you imported formgroup twice. I removed it in my app module, now it works for me, Hope it help someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM