热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何以编程方式将角2控件设置为dirty?-HowdoIprogrammaticallysetanAngular2formcontroltodirty?

HowdoImarkanAngular2Controlasdirtyinmycode?如何在代码中把角2控制标记为脏?WhenIdoitlikethis:当我

How do I mark an Angular 2 Control as dirty in my code?

如何在代码中把角2控制标记为脏?

When I do it like this:

当我这样做的时候:

control.dirty = true;

I get this error:

我得到这个错误:

Cannot set property dirty of # which has only a getter

3 个解决方案

#1


28  

You should use the markAsDirty method, like this:

您应该使用markAsDirty方法,如下所示:

control.markAsDirty();

This will also mark all direct ancestors as dirty to maintain the model.

这也将标记所有直接祖先为脏以维护模型。

Docs link

文档链接

#2


0  

For template driven forms we can use below generic code

对于模板驱动的表单,我们可以使用下面的通用代码

 public onSubmitForm(cardFormObject: NgForm) {
        if (!cardFormObject.valid)
            this.markAsDerty(cardFormObject);      

    }

    private markAsDerty(cardFormObject: NgForm) {
        for (var eachControl in cardFormObject.controls) {
            (cardFormObject.controls[eachControl]).markAsDirty();
        }
    }

#3


0  

You can write a custom function to mark all the controls in a FormGroup as touched/dirty like this:

您可以编写一个自定义函数,将FormGroup中的所有控件标记为如下所示:

  markFormGroupTouched(formGroup: FormGroup) {
    (Object).values(formGroup.controls).forEach(cOntrol=> {
      control.markAsDirty();  // or control.markAsTouched();
    });
  }

推荐阅读
author-avatar
似懂非懂2502904027
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有