0

我无法从中获得价值mat-datepicker。它说undefined。我用过ngFormngModel但我不知道我错在哪里。

<form #f="ngForm" (ngSubmit)="registerClient(f.value)">
    <div class="" style="width: 50%; margin:auto;">
        <!-- ... Other fields -->
        <!-- ... -->

        <div class="form-group">
            <mat-form-field id="date">
                <input matInput [matDatepicker]="picker" placeholder="Choose a date" [(ngModel)]="date">
                <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                <mat-datepicker id="datePicked" #picker></mat-datepicker>
            </mat-form-field>
        </div>
        <div>
        </div>
        <button class="btn btn-success mt-2">Add new client</button>
    </div>
</form>

客户端form.ts

export class ClientFormComponent {
    date: Date;
    // ...
    // ...
    registerClient(crediantials) {
        let client: Client = {
            firstName: crediantials.firstName,
            lastName: crediantials.lastName,
            phone: crediantials.phone,
            doctorsName: this.selectedD.username,
            procedure: this.selectedP,
            registrationDate: this.date
        };

        console.log(client), 'client';
        console.log(crediantials)
        console.log(this.date, ' date);
        this.clientService.addClient(client);
    }
}

这些是我在控制台中遇到的错误。

ClientFormComponent.html:41 ERROR 错误:如果在表单标签中使用 ngModel,则必须设置 name 属性或必须在 ngModelOptions 中将表单控件定义为“独立”。

示例 1:<input [(ngModel)]="person.firstName" name="first">

示例 2:<input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">

错误上下文 DebugContext_ {view: {…}, nodeIndex: 69, nodeDef: {…}, elDef: {…}, elView: {…}}

ERROR 错误:ViewDestroyedError:尝试使用已破坏的视图:detectChanges

这是发生错误的行。

<input matInput [matDatepicker]="picker" placeholder="Choose a date"  [(ngModel)]="date">
4

1 回答 1

0

如果使用 ngForm,则具有 ngModal 的输入字段必须具有带值的属性名称。

<input matInput [matDatepicker]="picker" name="date" placeholder="Choose a date" [(ngModel)]="date">
于 2019-09-23T10:09:10.060 回答