简体   繁体   English

如何以 Reactive 形式获取 Angular Dropdown 的值

[英]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.我在 Angular Reactive 表单中有一个下拉列表(状态),我不确定如何获取下拉列表更改的值。

This is the Angular Code in HTML:这是 HTML 中的 Angular 代码:

           <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:这是方法是 TS 文件,但即使 Console.log 也不起作用,意味着更改方法甚至没有调用此方法:

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)尝试 (ngModelChange) 而不是 (change)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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