What is updateValueAndValidity

前端 未结 3 695
忘掉有多难
忘掉有多难 2020-12-17 09:56

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

3条回答
  •  死守一世寂寞
    2020-12-17 10:14

    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.

提交回复
热议问题