TypeError:this.form._updateTreeValidity不是函数

时间:2016-11-30 20:57:00

标签: javascript angular validation typescript angular2-forms

我目前正在使用Angular Forms版本2.0.0,并尝试使用内部联系表单与联系我们模式。

在加载ContactComponent之后,我得到:

  

EXCEPTION:this.form._updateTreeValidity不是函数

Can't fine this.form._updateTreeValidity

  1. 我已经看过其他一些堆栈帖子,建议使用FormGroup而不是FormBuilder在组件构造函数中初始化表单对象现在是新API的标准,所以我已经更新了。< / p>

  2. 我导入了ReactiveFormsModule和FormsModule以及所有与表单相关的组件,并且错误似乎与模块无关。

  3. 我的TypeScript不会在编译时抛出错误,Visual Studio Intellisense似乎能够很好地找到所有FormGroup函数,为什么会在运行时发生这种情况?...

  4. 我的代码:

    contact.component.ts:

    import { Component, Input, ViewChild } from '@angular/core';
    import { ApiService } from '../../../services/api.service';
    import { ModalComponent } from 'ng2-bs3-modal/ng2-bs3-modal';
    import { Router, ActivatedRoute, Params } from '@angular/router';
    import { FormsModule, ReactiveFormsModule, FormGroup, FormControl, Validators } from '@angular/forms';
    import 'rxjs/Rx';
    
    declare var jQuery: any;
    
    @Component({
        selector: 'my-contact',
        templateUrl: 'app/modules/footer/contact/contact.html'
    })
    export class ContactComponent {
    
        private contactForm: FormGroup;
        private invalidEmail: boolean;
        private invalidSubject: boolean;
        private invalidMessage: boolean;
    
        constructor(private apiService: ApiService, private router: Router, private route: ActivatedRoute) {
            this.contactForm = new FormGroup({
                emailControl: new FormControl('', <any>Validators.required),
                subjectControl: new FormControl('', <any>Validators.required),
                messageControl: new FormControl('', <any>Validators.required)
            });
        }
    
        submit() {
    
            if (this.contactForm.valid) {
                this.apiService.sendMessage(this.contactForm.controls['emailControl'].value, this.contactForm.controls['subjectControl'].value, this.contactForm.controls['messageControl'].value);
            }
    
            if (!this.contactForm.controls['emailControl'].valid) {
                this.invalidEmail = true;
            }
            if (!this.contactForm.controls['subjectControl'].valid) {
                this.invalidSubject = true;
            }
            if (!this.contactForm.controls['messageControl'].valid) {
                this.invalidMessage = true;
            }
    
        }
    
        ngOnInit() {
            this.invalidEmail = false;
            this.invalidSubject = false;
            this.invalidMessage = false;
        }
    
    }
    

    contact.html:

    <modal-header class="c-no-border" [show-close]="true">
      <h4 class="modal-title text-uppercase">Send us a message</h4>
    </modal-header>
    
    <form novalidate #contactForm [formGroup]="contactForm" (ngSubmit)="submit()">
      <div class="modal-body">
        <div class="form-group">
          <label for="email" class="control-label">Email</label>
          <input name="email" formControlName="emailControl" placeholder="" type="text" class="c-square form-control c-margin-b-20" id="email">
          <div class="c-font-red-1" *ngIf="invalidEmail" style="position: absolute;">*Required</div>
          <label for="subject" class="control-label">Subject</label>
          <input name="subject" formControlName="subjectControl" placeholder="" type="text" class="c-square form-control c-margin-b-20" id="subject">
          <div class="c-font-red-1" *ngIf="invalidSubject" style="position: absolute;">*Required</div>
          <textarea formControlName="messageControl" style="resize: vertical;" class="c-square form-control c-margin-b-20" id="content" (keyup.enter)="submit()"></textarea>
          <div class="c-font-red-1" *ngIf="invalidMessage" style="position: absolute;">*Required</div>
        </div>
      </div>
      <modal-footer class="c-no-padding">
        <button type="button" class="btn c-btn-square c-btn-bold c-btn-uppercase pull-right">Cancel</button>
        <button type="submit" class="btn c-theme-btn c-btn-square c-btn-bold c-btn-uppercase pull-right" style="margin-right: 10px;">Send</button>
      </modal-footer>
    </form>
    

    app.module.ts:

    import { NgModule, enableProdMode }      from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { AppComponent } from './app.component';
    import { FormsModule, ReactiveFormsModule } from '@angular/forms';
    import { Ng2Bs3ModalModule } from 'ng2-bs3-modal/ng2-bs3-modal';
    import { QueuesModule }     from './modules/queues/queues.module';
    import { OrderModule }     from './modules/order/order.module';
    import { AccountModule } from './modules/account/account.module';
    import { AdminModule } from './modules/admin/admin.module';
    import { routing } from './app.routing';
    import { GridModule } from '@progress/kendo-angular-grid';
    import { SplashComponent } from './modules/splash/splash.component';
    import { ContactComponent } from './modules/footer/contact/contact.component';
    
    import { SharedModule } from './shared/shared.module';
    import { EmailValidator } from './shared/utilities/custom-validators'
    
    import { CookieService } from 'angular2-cookie/services/cookies.service';
    import { HttpModule, Response } from '@angular/http';
    import { StringService } from './services/string.service';
    import { ApiService } from './services/api.service';
    import { UserService } from './services/user.service';
    import { OrderService } from './services/order.service';
    import { OrderGuard } from './services/order-guard.service';
    import { FooterComponent } from './modules/footer/footer.component';
    import { ErrorComponent } from './modules/error/error.component';
    import { CustomFormsModule } from "ng2-validation";
    
    @NgModule({
        imports: [
            BrowserModule,
            FormsModule,
            ReactiveFormsModule,
            HttpModule,
            QueuesModule,
            OrderModule,
            AccountModule,
            AdminModule,
            routing,
            GridModule,
            SharedModule,
            Ng2Bs3ModalModule,
            CustomFormsModule
        ],
        declarations: [
            AppComponent,
            SplashComponent,
            FooterComponent,
            ErrorComponent,
            ContactComponent
        ],
        providers: [
            StringService,
            ApiService,
            UserService,
            CookieService,
            OrderService,
            OrderGuard
        ],
        bootstrap: [AppComponent],
        exports: [
        ]
    })
    
    export class AppModule {
    }
    

5 个答案:

答案 0 :(得分:35)

绑定模板变量#contactForm似乎会导致名称冲突并炸毁模板处理器,因为它试图将附加的模板变量转换为后端的NgForm。我所看到的模型驱动表单到处都没有模板上的模板变量绑定,而模板驱动表单中使用了#tv="ngForm"。似乎错过了两种形式方法的混合导致了错误。

只需删除它即可解决问题。

答案 1 :(得分:0)

我有一个动态的表格。由于没有在constructor()

中初始化它,因此出现了错误

解决方案:

checkInForm: FormGroup;

constructor(){
    this.checkInForm = this.formBuilder.group({});
}

答案 2 :(得分:0)

已经有一段时间了,但是对我来说,问题是将formGroup传递给模板,而不是直接引用它。

例如这是不起作用

<ng-template #selectField
             let-field
             let-group
             let-frmName="frmName">
    <ng-container [formGroup]="group">
        <mat-form-field fxFlex="32"
                        floatLabel="always">
            <mat-label>{{field.label}}</mat-label>
            <mat-select [formControlName]="frmName"
                        [required]="field.required">
                <mat-option *ngFor="let option of field.options"
                            [value]="option.value">
                    {{option.description}}
                </mat-option>
            </mat-select>
        </mat-form-field>
    </ng-container>
</ng-template>

如果我直接使用我的formGroup实例,它就可以正常工作。

希望这会有所帮助。

答案 3 :(得分:0)

当您在不是表单控件的自定义组件上使用输入名称formControlformGroup时,可能会发生这种情况。

更改自定义组件以接受其他输入名称:

更改此

@Input()
formGroup: string
// ^ Causes issues

对此

@Input()
group: string

答案 4 :(得分:0)

如果您错误地将 formGroup="..." 而不是 [formGroup]="..." 添加到模板中,您也会收到此错误消息。