arrays - 在 ngFor 中合并两个数组

标签 arrays angular merge

我有两个数组-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/

相关文章:

angular 6 child 路线组件未加载?

c# - 如何在 tableLayoutPanel 中设置 columnspan

git - 使用 Git 将更改从 master merge 到所有分支?

angular - 如何以 Angular 获取HttpClient模块的状态码

git - 线尾: Git merge creates duplicates without conflict

Javascript:检查数字数组中缺少的数字数量,以使数组连续

PHP - 从 sql 数据库调用的数组不能正确分解,但直接创建的数组可以吗?

c - while 循环中的 Free()

c++ - Counter int 在不询问的情况下递增

angularjs - Jest 和混合 Angular/AngularJS 应用程序