简体   繁体   English

如何在 angular 的每个下拉列表中显示 object 数组值 14

[英]How to show the object array value in the each dropdown in angular 14

Trying to show object array value in the dropdown.试图在下拉列表中显示 object 数组值。 I have one object and it will have 'Car', 'Bus', 'Cycle', 'MotorCycle', 'Train' array value.我有一个 object,它将有 'Car'、'Bus'、'Cycle'、'MotorCycle'、'Train' 数组值。 I want to show the value of this object in each dropdown.我想在每个下拉列表中显示这个 object 的值。 Like first dropdown Car array value, second dropdown Bus value, third dropdown Cycle, fourth dropdown MotorCycle value and fifth dropdown Train value.像第一个下拉汽车数组值、第二个下拉总线值、第三个下拉循环、第四个下拉摩托车值和第五个下拉火车值。 I have used input decorator.我用过输入装饰器。 So, How to show these value in each dropdown.那么,如何在每个下拉列表中显示这些值。

main.component.ts: main.component.ts:

export class MainComponent implements OnInit {
travals = {
Car: ['Good', 'Bad'],
Bus: ['Supper', 'Coll'],
Cycle: ['Small', 'Big'],
MotorCycle: ['Too Big', 'Scooty'],
Train: ['Special', 'Nomal', 'Super Fast'],
};

vehicle = ['Car', 'Bus', 'Cycle', 'MotorCycle', 'Train']; 
constructor() {} 
ngOnInit() {}
}

vehicle.component.ts:车辆组件.ts:

export class VehicleComponent implements OnInit {
@Input() data: any = []; 
constructor() {} 
ngOnInit() {
console.log(this.data);
}
}

Demo: https://stackblitz.com/edit/angular-ivy-1w4gxt?file=src%2Fapp%2Fshared%2Fvehicle%2Fvehicle.component.ts演示: https://stackblitz.com/edit/angular-ivy-1w4gxt?file=src%2Fapp%2Fshared%2Fvehicle%2Fvehicle.component.ts

Your vehicle.component.html :您的vehicle.component.html

<select *ngFor="let vehicleType of data | keyvalue" [attr.name]="vehicleType.key">
  <option value="{{val}}" *ngFor="let val of $any(vehicleType.value)">
    {{ val }}
  </option>
</select>

This should work.这应该有效。 keyvalue pipe allows to iterate objects and to access key value pairs. keyvalue pipe 允许迭代对象和访问键值对。 You can use them as you like in your particular task.您可以在特定任务中随意使用它们。

Link in documentation https://angular.io/api/common/KeyValuePipe文档中的链接https://angular.io/api/common/KeyValuePipe

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

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