我有两个数组-a,我从服务中得到:
handleProjectLoaded(project){
this.projectService.getCodesByType( "Structure" ).subscribe( response => {
this.structures = response;
});
this.projectService.getCodesByType( "Direction" ).subscribe( response => {
this.lines = response;
});
}
一个数组在结构中,第二个我在行中。我将它们分开放在 html 中,并显示了 ngFor。
<div class="div-width-button3">
<select #lineSelect class="custom-select custom-select-project" style="flex-basis:49%" (change)="addDirection(directionSelect.value); directionSelect.selectedIndex = 0;">
<option selected disabled value="">Add a direction</option>
<option *ngFor="let direction of directions" [value]=direction.id
[selected]="direction.id==directionSelect.value">{{direction.name}}</option>
</select>
<select #structureSelect class="custom-select custom-select-project" style="flex-basis:49%"
(change)="addStructure(structureSelect.value); structureSelect.selectedIndex = 0;">
<option selected disabled value="">Add a structure</option>
<option *ngFor="let structure of structures" [value]=structure.id
[selected]="structure.id==structureSelect.value">{{structure.name}}</option>
</select>
</div>
我需要一个解决方案来将这两个数组显示为一个 ngFor。有什么办法可以合并它们吗??
最佳答案
我没有测试过,但是为什么不用concat呢?
<option *ngFor="let line of lines.concat(structures)" [value]="line.id">{{ line.name }}</option>
关于arrays - 在 ngFor 中合并两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47167695/