Angular - 在尝试从html select标签获取option值时出现未定义值
创始人
2024-10-15 02:00:45
0

在Angular中,可以使用双向数据绑定和事件绑定来获取标签获取选项值:

在组件的HTML模板中:




在组件的typescript文件中:

import { Component } from '@angular/core';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css']
})
export class ExampleComponent {
  selectedOption: string;

  getSelectedOption() {
    console.log(this.selectedOption);
  }
}

在上述示例中,通过使用双向数据绑定[(ngModel)],将selectedOption属性与