- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有简单的形式
<form novalidate #f="ngForm">
<mat-form-field class="example-full-width">
<input matInput placeholder="Favorite food" name="myname" required [(ngModel)]="comment">
<mat-error>Is required lol</mat-error>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Favorite food" required name="some">
</mat-form-field>
</form>
export class InputOverviewExample {
comment = null;
@ViewChild('f')
form: NgForm
ngOnInit() {
console.log(this.form.controls);
console.log(Object.keys(this.form.controls));
console.log(this.form.controls.myname);
console.log(this.form.controls['myname']);
}
}
{}
myname: Object { pristine: true, touched: false, status: "INVALID", … }
Array []
undefined
Object.keys
返回空数组,而控制台说
form.controls
中有一些键??
最佳答案
你错过了 onInit 实现,
先加InputOverviewExample implements OnInit
正确实现和使用 ngOnInit
功能。
二、自 react 形式是同步的 , console.log
在表单获取之前运行 initialized
和之前 form control
被添加。
另外,一般实现 表单控件使用 form builder
在构造函数中,既然你没有接受,你就遇到了这个,所以下面的代码会为你解决
import { Component, ViewChild, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
/**
* @title Basic Inputs
*/
@Component({
selector: 'input-overview-example',
styleUrls: ['input-overview-example.css'],
templateUrl: 'input-overview-example.html',
})
export class InputOverviewExample implements OnInit {
comment = null;
@ViewChild('f')
form: NgForm
ngOnInit() {
setTimeout(() => {
console.log(this.form.controls);
console.log(typeof this.form.controls);
console.log(Object.keys(this.form.controls));
console.log(this.form.controls.myname);
console.log(this.form.controls['myname']);
})
}
}
The NgModel directive creates the FormControl instance to manage the template form control and the name attribute tells the NgModel directive what key to store that FormControl under in the parent FormGroup
<input name="foo" ngModel>
let model = new FormGroup({
"foo": new FormControl()
});
Console
executed first and later the value is assigned to it, so will not get access to any of the methods when value gets assigned after the execution of console.
关于angular - 为什么我无法从 NgForm 获得表单控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50267808/
在 Angular 1 (1.5) 中,表单 Controller 有一个 $setSubmitted()允许您以编程方式设置 .$submitted 的方法标记为真。 如何通过 NgForm 对象在
我是 Angular 的新手,我正在使用模板驱动的表单。当我使用 NgForm 时,它抛出错误: src/app/pages/add-booking/servicing/servicing.compo
我是 Angular 的新手,我正在使用模板驱动的表单。当我使用 NgForm 时,它抛出错误: src/app/pages/add-booking/servicing/servicing.compo
我正在尝试制作一个包含两个不同组件的表单。一位家长和一位 child 。我查看了很多教程,他们都在谈论提供服务。 不过,我在这个website上找到了一个方法.它包括在子组件中添加以下内容: view
我遇到了以下问题。我想使用 NG2 Forms。根据 Angular 2 文档,在表单上使用 ngForm 指令并在输入上使用 ngControl 指令,表单应该始终能够访问输入的有效性。 如果输入与
我有简单的形式 Is required lol 和组件作为 export class InputOverviewExample { comm
我最近一直在使用 React,并且非常喜欢它,但我错过了一些我在其他框架中习惯的东西。 ngForm 中主要的一个来自 Angular,Angular 使用它来提供非常简单的表单验证。检查表单是否有效
当我提交表单时,我有这个功能 login(form: NgForm) 如果我尝试以下操作 console.log(form.value.password); // this works perfect
我是 Angular 初学者,我正在尝试打印在控制台中选择的表单元素的值。所有其他元素都已打印,但 select 选定列表元素显示 undefined。这是我的代码category.services.
我正在尝试验证使用指令生成的动态表单输入并隔离范围。在指令之外,我有主窗体,在 ng-repeat 内部,我尝试使用 ng-form,但它从未显示错误消息。最初我在 ng-repeat 上有 ng-f
目标是当用户单击按钮转到下一页时,如果页面部分填写但无效,则阻止用户继续并显示错误指示符。 在模板驱动的表单中,我有几个容器元素使用 NgModelGroup 属性来表示表单中的“页面”。在我的组件中
我正在尝试做一个登录表单组件,但我无法读取表单数据。 当我尝试在控制台上写入用户名时,'undefined' 写入。 一切看起来都很正常,但表单数据没有到达组件。 下面是html代码:
这是父 HTML 这是子 HTML 我需要访问父组件中的子窗体状态 最佳答案 子模板: 子组件: export class Ch
login 工作组件的html代码是这样的。 Regis
我想知道是否有任何方法可以检索从表单的 NgForm.controls 对象中提取的 FormControl 的 NgModel它属于哪个,或者直接从表单中检索 NgModel。 我有一个表单,在提交
如何在组件中引用“myform”?是否可以?一定要使用 formbuilder(我尽量避免使用它)吗? Value 最佳答案 实际上还有另一种方法可以做到这一点。您可以使用
这个问题在这里已经有了答案: Angular 4.4.0-4.4.2 production build throwing "No provider for NgForm!" error (2 个答案
我在 Angular 4 上处理表单,我不知道使用 NgForm 和 FormGroup 处理错误和输入验证器有什么区别。处理表格有什么大的不同吗? 最佳答案 如果我没看错,您就会遇到模板驱动表单和响
我已经包含了 FormsModule 但它显示错误。 There is no directive with “exportAs” set to “ngForm” 我的 Login.component.
Angular 6 表单验证简单示例。通过电子邮件验证和选择框(下拉)。ngForm 带有选择框的 Angular 6 简单示例 最佳答案 使用ngForm。将其导入您的 component.modu
我是一名优秀的程序员,十分优秀!