简体   繁体   中英

How get the value of Angular Dropdown in Reactive form

I have a drop down (states) in Angular Reactive form, I am not sure how to get the value on change of the Drop down.

This is the Angular Code in HTML:

           <select 
                    formControlName="licensestate"
                    (change)="selectChangeHandler($event.target.value)"
                    label="Licensed State">
                <option>Licensed State</option>
                <option *ngFor="let state of (USAStates| enumKeyValue)"
                        [value]="state.key"> {{ state.value }}</option>
            </select>

and this is the Method is TS file, but even Console.log is not working, means the change method is not even calling this method:

public selectChangeHandler(event: any): void {
    console.log("test");
    console.log(event.target.value);
 }

Try like this:

  ngOnInit() {
    ...
    this.form.controls['licensestate'].valueChanges.subscribe((value) => {
        console.log(value);
    });
  }

Try (ngModelChange) instead of (change)

<select formControlName="licensestate" (ngModelChange)="selectChangeHandler($event)" label="Licensed State"> <option *ngFor="let state of (USAStates| enumKeyValue)" value="{{state.key}}">{{state.value}}</option> </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM