【发布时间】:2021-02-18 07:45:05
【问题描述】:
我创建了一个 angular 表单,并使用模板驱动的方法来控制它。已在组中使用 ngForm,并尝试通过名称访问表单字段,但在控制台引用时出现错误 - this.form 未定义。
我的 html 看起来像 -
<form #createProjectForm="ngForm" (ngSubmit)="save()">
.....
<mat-form-field appearance="outline">
<mat-label>NMC/ODA Project No</mat-label>
<input name="nmcNumbber" matInput autocomplete="off" />
</mat-form-field>
并且在组件文件中-
import { Component, OnInit, ViewChild } from '@angular/core';
import { NgForm, Form } from '@angular/forms';
@Component({
selector: 'app-create-project-details-fields',
templateUrl: './create-project-details-fields.component.html',
styleUrls: ['./create-project-details-fields.component.scss']
})
export class CreateProjectDetailsFieldsComponent implements OnInit {
@ViewChild('createProjectForm') createForm : NgForm;
constructor() { }
ngOnInit(): void {
this.getFormStatus();
}
getFormStatus(){
this.createForm.controls['nmcNumbber'].disable();
}
但是控制台给出了-
谁能帮我解决这个问题? TIA
【问题讨论】:
-
我将其更改为 ngAfterViewInit 但我仍然面临错误 -
-
我不认为这是创建表单的可靠方法。我认为您应该查看有关如何设置模板驱动表单的官方文档。或者只是代码没有显示所有组件。 angular.io/guide/…
标签: javascript angular