- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
下面是一个组件的代码,它在显示验证错误之前尝试确定 FormGroup
中的每个 AbstractControl
是否是 pristine
:
export class FieldErrorsComponent {
@Input() control: AbstractControl | FormGroup;
public controlsAreNotPristine(): boolean {
if (this.control instanceof FormGroup) {
return ((this.control as FormGroup).controls.every(control => !control.pristine));
}
}
}
当然,这不起作用,因为 FromGroup.controls
定义如下:
controls: { [key: string]: AbstractControl; };
我不知道在 controls
上循环有什么好的选择?真正的问题是 FormGroup.pristine
并没有真正反射(reflect)其子控件总和的值,据我所知,这可能是设计使然。
最佳答案
FormGroup
不提供任何迭代器作为 can be seen from its interface .但它提供了对如下定义的控件的访问:
controls: {[key: string]: AbstractControl}
因此您可以使用标准的 for in
循环来迭代它们:
public controlsAreNotPristine(): boolean {
if (this.control instanceof FormGroup) {
const controls = (this.control as FormGroup).controls
for (const name in controls) {
if (controls[name].pristine) {
return true;
}
}
return false;
}
}
The real problem is that FormGroup.pristine does not really reflect the value of the sum of its child controls, which I understand might be made so by design.
FormGroup 应该正确反射(reflect)其子控件的状态。从 FormGroup 的 _updatePristine
方法可以看出:
_updatePristine(opts: {onlySelf?: boolean} = {}): void {
this._pristine = !this._anyControlsDirty(); <------- ensure all controls are pristine
if (this._parent && !opts.onlySelf) {
this._parent._updatePristine(opts);
}
}
关于javascript - 在 Angular 的 FormGroup 中循环子 AbstractControl 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45209841/
我正在使用 angular rc4 版本来编写一个应用程序,在该应用程序中我必须强加一些验证规则来形成像 required 这样的组件。 , minlength以及一些自定义验证器 emailVali
我已经构建了一个表单,我在其中实现了表单验证,当用户将电子邮件或密码字段留空时,该表单仅显示一条错误消息。它工作正常,但我的问题是我将电子邮件和密码设置为“AbstractControl”而不是“St
我的错误页面没有按计划工作。我在我的 Application Module 中创建了一个事件与 onBootstrap处理填充我的设计 Assets 的方法。 它适用于所有页面,除了路由不匹配的页面我
我想在 Angular7 中构建表单数组 控件带有红色下划线。我什至在提供应用程序之前就遇到了这个错误: Property 'controls' does not exist on type 'Abs
我有以下代码: unless Rails.application.config.consider_all_requests_local rescue_from Exception, with: :
当我在用户 Controller 中调用此销毁方法时,出现以下错误。 AbstractController::DoubleRenderError (Render and/or redirect wer
我遇到了一个非常奇怪的路由错误,在 Rails 3.2.11 中我似乎无法解决该错误 错误是:AbstractController::ActionNotFound(无法为 SubmissionsCon
我必须从验证器指令内部触发验证。 这是我的指令。它按预期工作。但是我希望它在验证器函数更改时触发验证过程。 IE。当它的输入变量 maxDate 改变时。 我该怎么做? 如果我可以在构造函数中访问 A
当我尝试使用带有控件的表单时,出现此错误。 Type 'AbstractControl' is missing the following properties from type 'FormCon
我有这个自定义组件: 这里,控制定义为: @Input() control: FormControl; 我的组件的用法: this.myFormGroup = new FormGroup({
我遇到了一个双重渲染错误,我无法在模型的更新方法中摆脱它。 这是 Controller 的代码 class Admin::CmsHappeningNowFeatureController index
(Rails 3.0.7) 我的routes.rb有这个: namespace :admin do namespace :campus_hub do resources :billing_
我在 src/groovy/ssh 中有一个 AbstractController public void listAjax(){ //do sth } 然后创建 Controller syst
我遇到了一个双重渲染错误,我无法在模型的更新方法中摆脱它。 这是 Controller 的代码 class Admin::CmsHappeningNowFeatureController index
我有一个嵌套的 FormGroup,如下所示: this.formGroup: FormGroup: { // ... controls: { "GENERAL_AND
我正在 Angular 中进行表单验证,在阅读 API 时遇到了 nullValidator .它说它是一个“不执行任何操作的验证器”,这在我看来就像是一个实际上什么都不做的验证器。当我检查 code
已经针对此错误发布了很多博客,但没有针对 angular4 指定任何博客。 我在表单上添加和删除动态控件 在初始化期间向窗体添加控件 ngOnInit() { this
我正在学习在线类(class),但我有很多问题。 我取决于我把我的代码我得到 type 'abstractcontrol' is missing the following properties fr
今天,当我尝试为用户 Controller 使用一些辅助方法时,出现了此错误: AbstractController::DoubleRenderError (Render and/or redirec
我想为 select 元素中的每个选项元素创建一个自定义属性,但在创建可通过 AbstractControl 和 event.target 访问的属性时遇到问题。这是我所拥有的: 我尝试创建一个名为
我是一名优秀的程序员,十分优秀!