What is updateValueAndValidity

淺唱寂寞╮ 提交于 2019-12-18 18:52:41

问题


These docs state the following:

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).

What is this updateValueAndValidity?


回答1:


You can subscribe to value changes of a control or the whole form.

updateValueAndValidity allows you to modify the value of one or more form controls and the flag allows you to specify if you want this to emit the value to valueChanges subscribers.




回答2:


The sourcecode can be helpful to clear up exactly what it's doing:

https://github.com/angular/angular/blob/master/packages/forms/src/model.ts

Currently it seems to be doing the following (this list is based on method names):

  • 'Set initial status' - which makes .status 'VALID' except if ALL controls are disabled, in which case it makes it 'DISABLED'
  • 'Updates value' - this seems to set .value if the control is enabled, or clear it if disabled.
  • 'Runs validator' - this updates the whole error object. So custom errors would be cleared if you'd set any.
  • 'Cancel subscriptions' - stops any async validators running at the time
  • 'Emit' event - (if emitEvent != false in options). This is just the value and status normal form events.
  • Updates parent with same rules - unless onlySelf is set.

Note: it doesn't go down the tree, only up.

I wish they'd put something like this in the docs. They currently say 'Recalculates the value and validation status of the control.' which isn't particularly helpful.




回答3:


I concur with above answer. Just to add, all your changes related to control will be reflected only if you put this statement i.e

 updateValueAndValidity() 


来源:https://stackoverflow.com/questions/42197806/what-is-updatevalueandvalidity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!