【问题标题】:Stop Valuechanges of formcontrol and the formgroup停止formcontrol和formgroup的Valuechanges
【发布时间】:2017-08-12 17:51:23
【问题描述】:

我有一个带有表单控件的表单组。我已经订阅了 formgroup 和 formcontrol 的 valuechanges 事件。单击按钮我想禁用和重置 formcontrol 的值而不触发 valuechanges 所以我使用了emitEvent:false 它不会触发 formcontrol 的 valuechanges 但 formgroup 的 valuechanges 是被解雇了。我在这里有一个示例 plunker 演示 https://plnkr.co/edit/cN2wROc7o16w52ZEPZgH?p=preview 。这是预期的行为还是问题。有人可以指导我

  ResetAndDisable(){
    this.ParentGroup.controls['test'].reset(null,{emitEvent:false});
    this.ParentGroup.controls['test'].disable({emitEvent:false});
 }
 Enable(){
    this.ParentGroup.controls['test'].enable({emitEvent:false});
 }

【问题讨论】:

    标签: angular


    【解决方案1】:

    您可以使用emitEvent:falseonlySelf:true 的组合,其中onlySelf:true ...

    如果onlySelf 为真,则此更改只会影响此 FormControl 的验证,不会影响其父组件。默认为false

    那么你可以做的是:

    ResetAndDisable(){
      this.ParentGroup.get('test').reset(null,{onlySelf:true, emitEvent:false});
      this.ParentGroup.get('test').disable({onlySelf:true, emitEvent:false});
    }
    Enable(){
      this.ParentGroup.get('test').enable({onlySelf:true,  emitEvent:false});
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      • 2019-03-20
      • 2017-10-06
      • 2017-11-20
      • 1970-01-01
      相关资源
      最近更新 更多