Angular CLI 编译错误;预期 1 个参数,但得到 0

jpm*_*yob 5 typescript angular-cli

这是我的第一个 angular 项目,我一直在运输,使用内置服务器,偶尔进行构建以推送到服务器 - 现在我正在尝试 --prod 构建......并且我收到编译错误。

周四的大量阅读我已经设法解决了其中的大部分问题,并且我了解 AOT 的需求,但是这个错误让我感到困惑

我通过应用程序反复使用此代码来启动我的表单,

<form novalidate="novalidate"
    autocomplete="off"
    [formGroup]="form"
    (submit)="form.valid && submitForm()" >
Run Code Online (Sandbox Code Playgroud)

但在其中一些模板上我得到了

x.component.html(6,3): : Expected 1 arguments, but got 0.
Run Code Online (Sandbox Code Playgroud)

其中第 6 行是: (submit)="form.valid && submitForm()">

======== 注意:我已经将代码简化为一个简单的骨架

and included it here, so you see EXACTLY what is there...

no extrapolating or generalizing.

Builds fine in dev mode
Run Code Online (Sandbox Code Playgroud)

cli v(1.6.0)

我仍然得到同样的错误 - user-form.component.html(6,4): 预期 1 个参数,但得到 0。

所以我想我会给出完整的组件和模板。

组件.ts

import { Component, OnInit, EventEmitter, Output } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, NgForm, NgControl, Validators } from '@angular/forms';
import { AbstractControl } from '@angular/forms/src/model';
import { ValidationErrors } from '@angular/forms/src/directives/validators';

import { get } from 'lodash';

@Component({
    selector: 'user-form',
    templateUrl: './user-form.component.html',
})

export class UserFormComponent implements OnInit {

    form: FormGroup;
    loading = false;
    submitAttempted = false;

    constructor(
    ) { }

    get formDisabled () {
        return this.loading === true;
    }

    get xStatus () {
        const field = this.form.get('x');
        if (!field.touched) {
            return '';
        }
        if (field.errors && field.errors.required) {
            return 'This field is required';
        }
    }


    get formModel () {
        return {
            x: this.form.get('x').value
        }
    }

    ngOnInit() {
        this.form = new FormGroup({
            x: new FormControl(
                {
                    value: 'x',
                    disabled: this.formDisabled,
                },
                [
                    Validators.required
                ]
            )
        })
    }

    resetForm () {
        this.form.get('x').markAsUntouched();
        this.submitAttempted = false;
    }

    submitForm(form: NgForm) {
        console.log( this.form );
    }

    showError (fieldName: string) {
        const field = this.form.get(fieldName);
        return field.invalid && (field.touched || this.submitAttempted);
    }

}
Run Code Online (Sandbox Code Playgroud)

template.ts (user-form.component.html)

<form 
    novalidate="novalidate"
    autocomplete="off"
    [formGroup]="form"
    (ngSubmit)="submitForm()" >

    <label class="input" [class.state-error]="showError('x')">
        <input type="text" name="x" placeholder="x" formControlName="x" />
        <div *ngIf="xStatus" [innerHTML]="xStatus | safeHtml"></div>
    </label>


    <button type="submit" class="btn btn-primary" (click)="submitAttempted = true">
        <i *ngIf="loading" class='fa fa-spinner fa-spin '></i>
        {{ loading ? 'Saving' : 'Add x' }}
    </button>

</form>
Run Code Online (Sandbox Code Playgroud)

Gra*_*gon 4

您收到该错误是因为在表单类中,submitForm 需要表单作为参数,但在模板中您没有传递表单。

主观解决方案:不要这样做。只需使用submitForm() 来触发表单验证。没有理由将表单实例交还给自身。