Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FormControl statusChanges in angular2?

Tags:

angular

i am new to angular2 and i want to trigger a function when the user selects some value in a dropDown. So i tried to implement statusChange of the FormControl class but it is not getting triggered,

wonder how and when to use the statusChange in angular2 this is my code

class policy{
 subCategory: FormControl = new FormControl();
 ngOnInit() {
            this.subCategory.statusChanges.subscribe(
                s => {
                    alert('success');
                }, e => {
                    alert('error');
                }, () => {
                    alert('complete');
                }
            );
        }
}

I thought by implementing statusChanges i can trigger the success function on every change of value on the dropdown, obviously it is now working.

UPDATE 1

I have updated the plunkr

like image 497
Lijin Durairaj Avatar asked Feb 21 '17 06:02

Lijin Durairaj


Video Answer


1 Answers

As the comments stated, you probably wanted valueChanges instead. However, for the thousands of other folks who arrived here from searching for how statusChanges works, here you go:

this.subCategory.statusChanges.subscribe( (status) => {
  console.log(status); //status will be "VALID", "INVALID", "PENDING" or "DISABLED"
})

The docs describe those 4 possible values as follows:

  • VALID: This control has passed all validation checks.
  • INVALID: This control has failed at least one validation check.
  • PENDING: This control is in the midst of conducting a validation check.
  • DISABLED:This control is exempt from validation checks. These status values are mutually exclusive, so a control cannot be both valid AND invalid or invalid AND disabled.
like image 169
adamdport Avatar answered Oct 06 '22 10:10

adamdport