Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何迭代数组并在angular 2中的下拉选项中追加值?_Angular_Typescript_Angular2 Template - Fatal编程技术网

如何迭代数组并在angular 2中的下拉选项中追加值?

如何迭代数组并在angular 2中的下拉选项中追加值?,angular,typescript,angular2-template,Angular,Typescript,Angular2 Template,我使用下面的代码在数组上迭代,并在下拉选项中附加值 searchcomponent.html:- <div fxFlex.xs="100" class="ddlplatform" *ngFor="let dropdown of dropdownitems"> <select class="ddloption"> <option>{{dropdown}}</option> <option *ngFor="let

我使用下面的代码在数组上迭代,并在下拉选项中附加值

searchcomponent.html:-

<div fxFlex.xs="100" class="ddlplatform" *ngFor="let dropdown of dropdownitems">
   <select class="ddloption">
      <option>{{dropdown}}</option>
       <option *ngFor="let item of dropdown">{{item}}</option>
   </select>
</div> 
 OnOptionChange(evt) {
   this.dropdownitems = ['Platform', 'Category'];
   this.Platform = ['Online', 'Offline'];
   this.Category = ['2W', '3W', '4W'];         
 }

有两个下拉列表可用,即平台和类别。在平台下拉列表中,我想绑定联机和脱机选项。如何在下拉列表中动态绑定选项

您实际上可以在模板中使用
,以帮助通过组件的键引用组件的属性。通过利用此功能,您可以动态获取作为根属性的数组,并在
*ngFor
中使用它

<div fxFlex.xs="100" class="ddlplatform" *ngFor="let dropdown of dropdownitems">
  <select class="ddloption">
    <option>{{dropdown}}</option>
    <option *ngFor="let item of this[dropdown]" [value]="item">{{item}}</option>
  </select>
</div>
HTML:


{{dropdown.type}
{{item}}
OnOptionChange(evt) {
  this.dropdownItems = [ 
    { type: 'Platform', options: ['Online', 'Offline'] },
    { type: 'Category', options: ['2W', '3W', '4W'] }
  ];
}
<div fxFlex.xs="100" class="ddlplatform" *ngFor="let dropdown of dropdownitems">
  <select class="ddloption">
    <option>{{dropdown.type}}</option>
    <option *ngFor="let item of dropdown.options" [value]="item">{{item}}</option>
  </select>
</div>