Html 如何在angular 5中动态选择下拉列表?

Html 如何在angular 5中动态选择下拉列表?,html,angular,Html,Angular,我试图为angular 5实现一个选择选项,动态选择框取决于哪个后端组件发送选择 请注意,两个选择框是同一个框,只是选项内的值发生了变化 selector.html 因此,我的问题是如何使select选项动态地根据传入的标志选择selectorBs而不是selectorAs?您可以定义一个指向当前选项列表的变量: export class AppComponent{ choicesA = ["option 1", "option 2", "option 3"]; choicesB =

我试图为angular 5实现一个选择选项,动态选择框取决于哪个后端组件发送选择

请注意,两个选择框是同一个框,只是选项内的值发生了变化

selector.html


因此,我的问题是如何使select选项动态地根据传入的标志选择selectorBs而不是selectorAs?

您可以定义一个指向当前选项列表的变量:

export class AppComponent{
  choicesA = ["option 1", "option 2", "option 3"];
  choicesB = ["option A", "option B", "option C"];
  currentChoiceList: string[];

  processBackendData() {
    this.currentChoiceList = condition ? this.choicesA : this.choicesB;
  }
}
并在模板中使用它:

<select>
  <option *ngFor="let choice of currentChoiceList">{{choice}}</option>
</select>

{{choice}}
export class AppComponent{
  choicesA = ["option 1", "option 2", "option 3"];
  choicesB = ["option A", "option B", "option C"];
  currentChoiceList: string[];

  processBackendData() {
    this.currentChoiceList = condition ? this.choicesA : this.choicesB;
  }
}
<select>
  <option *ngFor="let choice of currentChoiceList">{{choice}}</option>
</select>