【问题标题】:Recieving error when creating form in Ionic and Angular在 Ionic 和 Angular 中创建表单时收到错误
【发布时间】:2020-07-10 00:45:20
【问题描述】:

我正在尝试使用 Angular 9.1.10 在 Ionic 6.10.1 中构建一个“简单”表单。

但我不断收到以下错误:

错误错误:未捕获(承诺):错误:NodeInjector:NOT_FOUND [ControlContainer]

我已经确认我在 FormsModule 和 ReactiveFormsModule 中导入,我在模板中定义了 formGroup,并将依赖项导入到组件中。基本上,涵盖了我可以通过 Google 找到的所有问题。我也重启了几次服务器,但都没有成功。

通过注释掉代码,我将其范围缩小到模板文件,但无法找出问题所在。

我希望有一双新的眼睛能帮上忙……

app.module.ts

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 { FormsModule, ReactiveFormsModule} from '@angular/forms';

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

home.page.ts

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

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})

export class HomePage implements OnInit{

  type:Array<object> =[ 
    {name: 'mulch',
    weightPerYard: 700},
    {name: 'mravel',
    weightPerYard: 2600},
    {name: 'sand',
    weightPerYard: 2700},
    {name: 'topsoil',
    weightPerYard: 2000},
  ];

  calc: FormGroup;

  totalYards:Number = 0;

  totalTons:Number = 0;


  constructor(private form:FormBuilder) {  

  }

  ngOnInit() {
    this.calc = this.form.group({
      type: ['', [Validators.required]],
      width: ['', [Validators.required, Validators.pattern('^[0-9]+$')]],
      length: ['', [Validators.required, Validators.pattern('^[0-9]+$')]],
      depth: ['', [Validators.required, Validators.pattern('^[0-9]+$')]]
    })
  }

  calculateWeight(){
    console.log("TEST");
    
    
  }

}

形成 HTML 代码

<form [formGroup]="calc" (ngSubmit)="calculateWeight()" id="calc-form">
  <ion-item>
    <ion-label>Type of Material: </ion-label>
    <ion-select oktext="Ok"   formControlName="type" name="type" dismissText="Dismiss"  >
      <ion-select-option value="mulch">Mulch</ion-select-option>
      <ion-select-option value="gravel">Gravel</ion-select-option>
      <ion-select-option value="sand">Sand</ion-select-option>
      <ion-select-option value="topsoil">Top Soil</ion-select-option>
    </ion-select> 

  </ion-item>

  <ion-item>
    <ion-label>Width (sq feet): </ion-label>
    <ion-input type="number"  clearInput formControlName="width" name="width" ></ion-input>
  </ion-item>

    <ion-item>
      <ion-label>Length (sq feet): </ion-label>
      <ion-input type="number"  clearInput formControlName="length" name="length"  ></ion-input>
    </ion-item>

      <ion-item>
        <ion-label>Depth (inches): </ion-label>
        <ion-input type="number"  clearInput  formControlName="depth" name="depth"  ></ion-input>
      </ion-item>

      <ion-button expand="block" strong size="large" >Submit</ion-button>

    </form>

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    如果你有 home.module.ts,你必须声明它

    import { FormsModule, ReactiveFormsModule} from '@angular/forms';
    

    在“导入”中也是如此。

    【讨论】:

    • 我没有意识到我的 HomePage 目录中有一个 module.ts 文件。这对我帮助很大。
    猜你喜欢
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    相关资源
    最近更新 更多