【问题标题】:How to disable emitEvent to all form controls如何对所有表单控件禁用 emitEvent
【发布时间】:2017-10-11 07:40:12
【问题描述】:

有没有办法将所有表单控件的 emitEvent 设置为 false ?

目前,当我们patchValue或setValue时,可以通过选项不抛出表单的valueChanges:

form.get['myControlName'].setValue('newValue', {emitEvent:false})
form.get['myControlName'].patchValue('newValue', {emitEvent:false})

但是如果我们有很多patchValue或setValue,那就有点重复了……我有办法在之前禁用emitEvent,更改所有值并在之后再次激活它?

【问题讨论】:

    标签: forms angular


    【解决方案1】:

    您可以使用这样的简单解决方案: 迭代你表单的所有控件并重置它们的值并将emitEvent设置为false

    //the "manual" solution :
    form.controls['name'].setValue('nameNewValue', {emitEvent:false});
    form.controls['address'].setValue('addressNewValue', {emitEvent:false});
    
    // the "dynamic" solution
    for(let control in form.controls){            
        form.controls[control].setValue(form.controls[control].value, {emitEvent:false});
    }
    

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2018-03-02
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多