【问题标题】:how do I dynamically add options to a select tag based on array data?如何根据数组数据动态地将选项添加到选择标签?
【发布时间】:2021-06-26 06:29:01
【问题描述】:

这就是我所拥有的

mypage.component.html

<select>
    <option>2 weeks</option>
    <option>1 Month </option>
    <option>6 Months</option>
</select>

mypage.component.ts

export class MyPageComponent implements OnInit {
    constructor() { }
    public searchDates: Array<String> = [ "1 week", "2 weeks" ];

ngOnInit(): void { }
}

如何用 searchDates 数组中的内容替换硬编码选项?假设数组的大小会不时改变。

【问题讨论】:

    标签: html angular typescript


    【解决方案1】:

    使用ngFor:

    <select>
        <option *ngFor="let a of searchDates">
            {{a}}
        </option>
    </select>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多