Angular2, by default, checks that the control form / group form is cascaded to the top level when there is an update to the value of the form element, unless you say no. onlySelf is a tool to help you with this.
Say you have a loginForm that has a username field and a password field, both of which are necessary, for example:
this.userNameControl = this.formBuilder.control('Harry', Validators.required); this.passwordControl = this.formBuilder.control('S3cReT', Validators.required); this.loginForm = this.formBuilder.group({ userName: this.userNameControl, password: this.passwordControl });
After this code, this.loginForm.valid is true .
If you set the control value using the default value ( onlySelf = false ), Angular2 will update the correctness of the action, as well as the validity of the form group. For example, this:
this.passwordControl.setValue('');
will result in
this.passwordControl.valid === false this.loginForm.valid === false
However, this:
this.passwordControl.setValue('', { onlySelf: true });
only changes the validity of passwordControl :
this.passwordControl.valid === false this.loginForm.valid === true
Harry ninh
source share