gpt4 book ai didi

javascript - Angular 2 错误 : Can't resolve all parameters for FormGroup

转载 作者:太空狗 更新时间:2023-10-29 17:50:26 24 4
gpt4 key购买 nike

我正在关注 Thoughtram 的 FormBuilder 教程 http://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html我复制了代码并更改了几个变量名。我正在使用 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

HTML 示例 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>

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

在我尝试这个响应式表单代码之前,在 apartment4Rent.component.html 加载和出现之间。我的目录结构是app/components/input/apartment4Rent.component(s)

要使响应式表单发挥作用,必须进行哪些更改?

最佳答案

您是否将 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 的导入

关于javascript - Angular 2 错误 : Can't resolve all parameters for FormGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40537628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com