角FormControl有一个可观察到的valueChanges,即:
每当控件的值在UI中或以编程方式更改时,都会发出一个事件。
是否有一种方法可以将FormControl设置为忽略编程值更改?(基本上相当于OneWayToSource在.NET中的绑定)
具体来说,我面临的问题是我的valueChanges订阅,我正在更新绑定到一组其他控件的值,这也会导致valueChanges为所有控件触发,这是有问题的,因为它们在valueChanges处理程序中执行的操作与用户实际接触的控件冲突。
发布于 2018-04-16 15:54:35
通过将选项{ emitEvent: false }传递给setValue调用,可以跳过发出setValue事件。
setValue(value: any, options: {
onlySelf?: boolean;
emitEvent?: boolean;
emitModelToViewChange?: boolean;
emitViewToModelChange?: boolean;
} = {}): void此外,您可能还想看看其他选项。
如果onlySelf为真,则此更改只会影响此FormControl的验证,而不会影响其父组件的验证。这默认为false。 如果emitEvent为真,则此更改将导致在FormControl上发出一个valueChanges事件。这默认为true (因为它通过updateValueAndValidity)。 如果emitModelToViewChange为真,则将通过onChange事件通知视图新值。如果未指定emitModelToViewChange,则这是默认行为。 如果emitViewToModelChange为真,将触发一个ngModelChange事件来更新模型。如果未指定emitViewToModelChange,则这是默认行为。
发布于 2020-05-15 08:28:11
可以将{ emitEvent: false }作为下面的反应性表单方法的选项传递,以防止它们触发valueChanges事件。
this.form.patchValue(value, { emitEvent: false })this.form.setValue(value, { emitEvent: false })this.form.controls.email.updateValueAndValidity({ emitEvent: false })this.form.disable({ emitEvent: false })是的,禁用触发valueChanges事件
PS:上面的this.form是一种反应形式
阅读这篇优秀的文章,它会回答你所有的问题,甚至给出一些关于反应形式的深刻见解:
https://netbasal.com/angular-reactive-forms-tips-and-tricks-bb0c85400b58
发布于 2019-10-07 13:28:26
反应型群具有yourFormName.pristine性质
您可以使用!yourFormName.pristine只检测UI更改。
* A control is `pristine` if the user has not yet changed
* the value in the UI.
*
* @returns True if the user has not yet changed the value in the UI; compare `dirty`.
* Programmatic changes to a control's value do not mark it dirty.
*/
readonly pristine: boolean;https://stackoverflow.com/questions/49861281
复制相似问题