Angular FormControl
有一个 valueChanges
Observable 声明:
Emits an event every time the value of the control changes, in the UI or programmatically.
有没有办法将 FormControl 设置为忽略程序值更改? (基本上等同于 .NET 中的 OneWayToSource
绑定(bind))
具体来说,我面临的问题是我的 valueChanges
订阅,我正在更新一堆绑定(bind)到一堆其他控件的值,然后导致 valueChanges
也为所有这些触发,这是有问题的,因为它们在 valueChanges
处理程序中执行的操作与用户实际触摸的控件冲突。
最佳答案
您可以通过将选项 { emitEvent: false }
传递给 setValue
调用来跳过发出 valueChange 事件。
setValue(value: any, options: {
onlySelf?: boolean;
emitEvent?: boolean;
emitModelToViewChange?: boolean;
emitViewToModelChange?: boolean;
} = {}): void
您可能还想看看其他选项。
If onlySelf is true, this change will only affect the validation of this FormControl and not its parent component. This defaults to false.
If emitEvent is true, this change will cause a valueChanges event on the FormControl to be emitted. This defaults to true (as it falls through to updateValueAndValidity).
If emitModelToViewChange is true, the view will be notified about the new value via an onChange event. This is the default behavior if emitModelToViewChange is not specified.
If emitViewToModelChange is true, an ngModelChange event will be fired to update the model. This is the default behavior if emitViewToModelChange is not specified.
关于Angular Reactive Forms valueChanges - 仅 UI 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49861281/