在Angular 6中使用模板驱动的表单,可以通过使用ngModel来实现动态选择。下面是一个示例代码:
在模板中:
在组件中:
import { Component } from '@angular/core';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent {
selectedOption: string;
options: string[] = ['Option 1', 'Option 2', 'Option 3'];
constructor() { }
}
这里我们使用ngModel指令将选择的值绑定到selectedOption属性上。通过遍历options数组,将每个选项渲染为select元素的一个option。
你可以根据需要修改选项的数据和模板。当用户选择一个选项时,selectedOption属性将被更新为所选选项的值。