简体   繁体   English

Angular 2.0 和 TypeScript - 填充<select>多维数组对象的选项

[英]Angular 2.0 and TypeScript - Populate <select> options from multi-dimensional array objects

I would like to populate the <select name="selectmodel"> <option> from a nested array of objects based on the selection of the <select name="selectmake"> <option> element.我想根据<select name="selectmake"> <option>元素的选择从嵌套的对象数组中填充<select name="selectmodel"> <option>

Here is the multi-dimensional array:这是多维数组:

muscleCars = [
    {
      id: 1, name: "Chevrolet", models: [
        { model: "Camaro" },
        { model: "Corvette" }
      ]
    },
    {
      id: 2, name: "Dodge", models: [
        { model: "Charger" },
        { model: "Challenger" },
        { model: "Viper" }
      ]
    },
    {
      id: 3, name: "Ford", models: [
        { model: "GT" },
        { model: "Mustang" }
      ]
    }
];

This is the HTML这是 HTML

//select for Make:
<select name="selectmake" [(ngModel)]="makeListFilter">
    <option *ngFor="let muscleCar of muscleCars" [ngValue]="muscleCar.name">{{muscleCar.name}}</option>
</select>

//select for Model:
<select name="selectmodel" [(ngModel)]="modelListFilter">
    <option *ngFor="let muscleCar of muscleCars" [ngValue]="muscleCar.models">{{muscleCar.models}}</option>
</select>

So, basically when you select Chevrolet for example, I would like to have the second element populated with Camaro and Corvette .因此,基本上当您选择 Chevrolet 时,我希望第二个元素填充CamaroCorvette

Currently, the second select element is populated with an array [object Object] for each make, but can't figure out how to dig this deeper.目前,第二个select元素为每个 make 填充了一个数组[object Object] ,但无法弄清楚如何更深入地挖掘它。

在此处输入图片说明

Here is a plunk: https://embed.plnkr.co/0eEIJg5uzL6KsI70wWsC/这是一个 plunk: https ://embed.plnkr.co/0eEIJg5uzL6KsI70wWsC/

Any help would be appreciated.任何帮助,将不胜感激。

This is how your HTML should look like:这是您的 HTML 的外观:

<select name="selectmake" [(ngModel)]="makeListFilter">
    <option *ngFor="let muscleCar of muscleCars" [ngValue]="muscleCar">{{muscleCar.name}}</option>
</select>

<select name="selectmodel" [(ngModel)]="modelListFilter">
    <option *ngFor="let carModel of makeListFilter?.models" [ngValue]="carModel.model">{{carModel.model}}</option>
</select>

So, what's happening here is that selected value of selectmake dropdown is binded to makeListFilter and second dropdown selectmodel is populated based on selected value of first dropdown.所以,这里发生的事情是selectmake下拉列表的选定值绑定到makeListFilter并且第二个下拉列表selectmodel是根据第一个下拉列表的选定值填充的。 You will notice I binded the whole Object that is selected in first dropdown using ngValue directive so it can be used to populate second dropdown.您会注意到我使用ngValue指令绑定了在第一个下拉列表中选择的整个Object ,以便它可以用于填充第二个下拉列表。 Another interesting thing you'll notice is Elvis operator ( ? ) I used in second dropdown - it is used to tell second dropdown to populate itself only after value is selected in first dropdown, this is necessary to avoid getting error for iterating through an undefined value.您会注意到的另一件有趣的事情是我在第二个下拉列表中使用的Elvis运算符 ( ? ) - 它用于告诉第二个下拉列表仅在第一个下拉列表中选择值后才填充自身,这是必要的,以避免在迭代undefined出错价值。 If you don't want to use Elvis operator, you can use *ngIf directive to prevent getting mentioned error, but that means that second dropdown will appear only after you select something in the first dropdown:如果您不想使用Elvis运算符,则可以使用*ngIf指令来防止出现提及错误,但这意味着只有在您在第一个下拉列表中选择某些内容后才会出现第二个下拉列表:

<select *ngIf="makeListFilter" name="selectmodel" [(ngModel)]="modelListFilter">
    <option *ngFor="let carModel of makeListFilter.models" [ngValue]="carModel.model">{{carModel.model}}</option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM