Angular Reactive Forms valueChanges - 仅 UI 更改

标签 angular angular-reactive-forms

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.

Docs

关于Angular Reactive Forms valueChanges - 仅 UI 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49861281/

相关文章:

angular - 如何在组件销毁时销毁响应式(Reactive) FormControl?

javascript - 发送自定义表单控件验证器 FormArray

angular - 以 Angular 上传文件的补丁值

angular - 如何编写 Angular Material 表单控件组件

javascript - 如何在 Angular 8 项目中使用 Satellite.js

angular - 具有 30 000 行的 mat-table 的性能

Angular react 形式正在被提交而无需提交

angular - 访问 FormArray 中控件的模板内的验证器状态

javascript - 获得两者 - 下载链接和链接以使用 aws sdk 从 s3 存储桶在浏览器中查看

angular - 错误 : No provider for t