【发布时间】:2020-02-22 15:31:34
【问题描述】:
所以 - 我有一组像这样的数据:
people = [
{
name: "Bob",
age: "27",
occupation: "Painter"
},
{
name: "Barry",
age: "35",
occupation: "Shop Assistant"
},
{
name: "Marvin",
age: "42",
occupation: "Mechanic"
},
{
name: "Arthur dent",
age: "27",
occupation: "Human"
},
然后我的 html 中也有一个下拉列表 -
<select id='peeps' name='people'>
<option></option>
</select>
<div class='show-info'></div>
这一切都在一个组件中,我要做的是遍历人员数组,用他们的名字填充选项,当您在下拉列表中选择该人时,他们的信息将显示在 div 中。我试图开始这个,但我遇到了一些问题。
我开始这样做了 -
peepsSelect = document.getElementById("peeps") as HTMLElement;
populationDropdown() {
for(var i = 0; i < this.people.length; i++) {
var option = document.createElement("option");
option.text = this.people[i].name;
option.value = this.people[i].name;
option.value = this.people[i].age;
option.value = this.people[i].occupation;
this.peepsSelect.add(option);
}
}
但是我收到了错误消息,例如'add does not exist on type htmlelement.
【问题讨论】:
-
这不是您在 Angular 中应该这样做的方式(没有 getElementById 等)。例如:stackoverflow.com/questions/35945001/…
标签: javascript html angular typescript