Arrays 在ngFor中合并两个数组
我从服务中获得了两个阵列a:Arrays 在ngFor中合并两个数组,arrays,angular,merge,Arrays,Angular,Merge,我从服务中获得了两个阵列a: handleProjectLoaded(project){ this.projectService.getCodesByType( "Structure" ).subscribe( response => { this.structures = response; }); this.projectService.getCodesByType( "Direction" ).subscribe( response =&
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>
添加方向
{{direction.name}
添加一个结构
{{structure.name}
我需要一个解决方案来将这两个阵列显示为一个阵列和一个ngFor。有什么方法可以合并它们吗?我没有测试它,但是为什么不使用concat呢
{{line.name}}
==编辑===
事实上,我改变了主意,因为我认为我上一个例子中的structuresAndLines是一个2d数组。因此,请尝试以下方法:
handleProjectLoaded(project){
Observable.zip(
this.projectService.getCodesByType( "Structure" ),
this.projectService.getCodesByType( "Line" ),
(structures, lines) => structures.concat(lines),
)
.subscribe(response => {
this.structuresAndLines = response;
});
}
使用Observable.forkjoin您可以在NGO上使用一个管道来传递两个数组。我也会尝试一下,从长远来看,这似乎更适合我。谢谢
handleProjectLoaded(project){
Observable.zip(
this.projectService.getCodesByType( "Structure" ),
this.projectService.getCodesByType( "Line" ),
(structures, lines) => structures.concat(lines),
)
.subscribe(response => {
this.structuresAndLines = response;
});
}