简体   繁体   English

Angular2 反应式表单使应用程序崩溃

[英]Angular2 Reactive forms crashes the app

We are working on a Angular2 Projekt, based off mgechev's Angular2 Seed , trying to use reactive forms to bind the form to data, however Angular2 throws strange errors.我们正在开发基于 mgechev 的Angular2 Seed的 Angular2 Projekt,尝试使用反应式表单将表单绑定到数据,但是 Angular2 会引发奇怪的错误。 The base idea, which we want to use, is the one documented here on scotch.io as ComplexForm under the headline "Model Driven (Reactive) Forms".我们想要使用的基本思想是在 scotch.io 上作为 ComplexForm 在标题“模型驱动(反应式)表单”下记录的思想。 Our code regarding the form is the following:我们关于表单的代码如下:

app.module.ts app.module.ts

@NgModule({
 imports: [BrowserModule, HttpModule, AppRoutingModule, AboutModule, HomeModule, ExaminerThesisListModule,
    ExaminerOverviewModule, ExaminerCreateThesisModule, StudentOverviewModule, SharedModule.forRoot(), FormsModule,
    ReactiveFormsModule, CommonModule],
  declarations: [AppComponent],
  providers: [{
    provide: APP_BASE_HREF,
    useValue: '<%= APP_BASE %>'
  }],
  bootstrap: [AppComponent]

})
export class AppModule { }

component html组件 html

<form [formGroup]="formData">
  <div class="well">aa</div>
</form>

Note: The app doesn't fail to run, when the [formGroup]="formData" attribute is removed.注意:当删除[formGroup]="formData"属性时,应用程序不会无法运行。 And yes, the html currently contains no input fields, we've commented them out to check if it was them making the app fail, but apparently there seems to be a more basic trivial problem we can't figure out.是的,html 当前不包含输入字段,我们已经将它们注释掉以检查是否是它们导致应用程序失败,但显然似乎有一个更基本的琐碎问题我们无法弄清楚。 One of the input fields looks like this and should later be read to the form-tag:输入字段之一如下所示,稍后应读取到表单标签:

 <div class="form-group necessary">
  <label class="input-heading">Intern oder Extern?</label>

  <div class="radio">
    <label>
      <input type="radio" name="externalInternal" value="internal" [formControl]="formData.controls['externalInternal']"> Interne Abschlussarbeit
    </label>
  </div>

  <div class="radio">
    <label>
      <input type="radio" name="externalInternal" value="external-faculty" [formControl]="formData.controls['externalInternal']"> Abschlussarbeit an externer Fakultät
    </label>
  </div>

  <div class="radio">
    <label>
      <input type="radio" name="externalInternal" value="external-company" [formControl]="formData.controls['externalInternal']"> Abschlussarbeit an externer Firma
    </label>
  </div>
</div>

The actual component typescript code looks like this:实际的组件打字稿代码如下所示:

import {Component, OnInit} from '@angular/core';

import { ThesesService } from '../shared/theses/theses.service';
import { Thesis } from '../shared/theses/thesis.model';
import {FormBuilder, FormGroup} from "@angular/forms";

/**
 * This class represents the lazy loaded ExaminerOverviewComponent.
 */
@Component({
  moduleId: module.id,
  selector: 'examiner-create-thesis',
  templateUrl: 'examiner-create-thesis.component.html',
  styleUrls: ['examiner-create-thesis.component.css']
})
export class ExaminerCreateThesisComponent implements OnInit {

  formData: FormGroup;

  ngOnInit() {
    this.formData = this.fb.group({
      "thesisType": "bachelor",
      "externalInternal": "internal",
      "titleGerman": "testg",
      "titleEnglish": "teste"
    });
  }

  constructor(private thesesService:ThesesService, private fb: FormBuilder) { };

  submit(formVal: any) {
    console.log("SUBMIT", formVal);
  }
}

The error message we get is a我们得到的错误信息是

Unhandled Promise rejection: cannot set property 'stack' of undefined..."未处理的承诺拒绝:无法设置未定义的属性“堆栈”......”

error, from which I can't find any helpful information about our code.错误,从中我找不到有关我们代码的任何有用信息。 I've uploaded a screenshot of the full error stack here .我上传完整的错误堆栈的截图在这里

EDIT: Kostadinov's suggestion of updating zone.js improved the problem by making zone.js returning a more helpful error response, which I've uploaded here .编辑:Kostadinov 的更新 zone.js 的建议通过使 zone.js 返回更有用的错误响应来改善问题,我已在此处上传。

This problem seems to be the same as documented in this problem , but the solution is already implemented in my code: Their problem was that the [formGroup] directive could not be found as it is implemented by ReactiveFormsModule, but this Module is already imported in AppModule properly, so this should not be causing a problem.这个问题似乎是与在记录这个问题,但解决的办法是在我的代码已经实现了:他们的问题是,[formGroup]指令找不到,因为它是由ReactiveFormsModule实现,但该模块是在已经导入AppModule 正确,所以这应该不会导致问题。

Apparently this is a bug in Zone.js: https://github.com/angular/angular-cli/issues/3975 .显然这是 Zone.js 中的一个错误: https : //github.com/angular/angular-cli/issues/3975 Just update to the latest version with:只需更新到最新版本:

npm install --save zone.js@latest

or explicitly:或明确:

npm install --save zone.js@0.7.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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