gpt4 book ai didi

angular - 在 Ionic 中使用表单会引发错误 : NodeInjector: NOT_FOUND [ControlContainer]

转载 作者:行者123 更新时间:2023-12-01 21:35:20 25 4
gpt4 key购买 nike

我创建了一个带有表单的 Ionic 应用程序。没什么特别的(代码在下面)。在我添加一个表单并使用表单组和其他东西之前,它工作得很好。当我开始添加表单组和所有连接的东西时,我得到这个错误:

core.js:6260 ERROR Error: Uncaught (in promise): Error: NodeInjector: NOT_FOUND [ControlContainer] Error: NodeInjector: NOT_FOUND [ControlContainer] at getOrCreateInjectable (core.js:5894) at Module.ɵɵdirectiveInject (core.js:21115) at NodeInjectorFactory.NgControlStatusGroup_Factory [as factory] (forms.js:1073) at getNodeInjectable (core.js:6025) at instantiateAllDirectives (core.js:12953) at createDirectivesInstances (core.js:12169) at ɵɵelementStart (core.js:21298) at EditPage_Template (template.html:18) at executeTemplate (core.js:12129) at renderView (core.js:11899) at resolvePromise (zone-evergreen.js:798) at resolvePromise (zone-evergreen.js:750) at zone-evergreen.js:860 at ZoneDelegate.invokeTask (zone-evergreen.js:399) at Object.onInvokeTask (core.js:41640) at ZoneDelegate.invokeTask (zone-evergreen.js:398) at Zone.runTask (zone-evergreen.js:167) at drainMicroTaskQueue (zone-evergreen.js:569)

我尝试了我在网上找到的东西:从文件 app.module.ts 中的 @angular/forms 导入 Forms Module 和 ReactiveFormsModule

这对我不起作用,我一直收到错误。

我在 SO 上找到并尝试过的链接,但没有用:

Getting error suddenly in Angular Error: NodeInjector: NOT_FOUND [ControlContainer]

No provider for ControlContainer Error while using Angular Material Forms with Angular 6

No provider for ControlContainer and No provider for ControlContainer

这是我的代码:

<ion-content>
<form [formGroup]="todo" (ngSubmit)="save()">
<ion-row>
<ion-col>
<ion-list inset>
<ion-item>
<ion-input placeholder="Title" formControlName="title" id="title" type="text"></ion-input>
</ion-item>
<ion-item>
<ion-input placeholder="Description" formControlName="description" id="description" type="text"></ion-input>
</ion-item>
</ion-list>
</ion-col>
</ion-row>
</form>
</ion-content>

在我的组件中:

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

@Component({
selector: 'app-edit',
templateUrl: './edit.page.html',
styleUrls: ['./edit.page.scss'],
})
export class EditPage {
editId: number = 0;
todo : FormGroup;

constructor(private formBuilder: FormBuilder) {

this.todo = this.formBuilder.group({
title: ['', Validators.required],
description: [''],
});
}
}

最后是我的 app.module:

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { HttpClientService } from './core/services/http-client.service';
import { HttpClientModule } from '@angular/common/http';
import { CommonModule } from '@angular/common';

@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports:
[
FormsModule,
ReactiveFormsModule,
CommonModule,
BrowserModule,
HttpClientModule,
IonicModule.forRoot(), AppRoutingModule
],
providers: [
StatusBar,
SplashScreen,
HttpClientService,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {
}

最佳答案

您必须在页面的 module.ts 文件中导入 ReactiveFormsModule

例如

import { IonicModule } from '@ionic/angular';
import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import { ProfilePage } from './profile.page';

@NgModule({
imports: [
IonicModule,
CommonModule,
FormsModule,
RouterModule.forChild([{path: '', component: ProfilePage}]),
ReactiveFormsModule
],
declarations: [ProfilePage]
})
export class ProfilePageModule {}

关于angular - 在 Ionic 中使用表单会引发错误 : NodeInjector: NOT_FOUND [ControlContainer],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62038446/

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