简体   繁体   English

如何使用 Angular 中的 ID 名称从数组中获取值?

[英]How to get a value from an array using ID name in Angular?

I am getting value form API graduated and I want to print original text from below array.我从graduated中获得价值,我想从下面的数组中打印原始文本。

API Response API 响应

在此处输入图像描述

Current Result as below image当前结果如下图

在此处输入图像描述

.ts file .ts 文件

 levelEducation = [
    {id: "not-graduated", name: "OTHERS.NotGraduated"},
    {id: "graduated", name: "OTHERS.Graduation"},
    {id: "master", name: "OTHERS.Master"},
    {id: "doctorate", name: "OTHERS.Doctorate"},
  ];

HTML file code HTML文件代码

 <div class="form-field">
     <label>{{'JOBAPPLICATION.LevelOfEducation' | translate}}</label>
     <h5>{{userData.livello_studi ? userData.livello_studi : '-'}}</h5>
     <h5 *ngFor="let item of levelEducation">{{(item.name ? item.name : '-' ) | translate}}</h5>
 </div>

So I need to value "OTHERS.Graduation" using ID name graduated in angular.所以我需要使用在 angular 中graduated的 ID 名称来评估“OTHERS.Graduation”。

If you want to map the values from graduated to Graduation you can solve this with a map.如果你想要 map 从毕业到毕业的值,你可以用 map 解决这个问题。 Assumed you have n values and this is not an editable input.假设您有 n 个值,并且这不是可编辑的输入。

let map = new Map();
map.set("graduated" ","Graduation");
map.set("not-graduated","Not graduated");

.... ……

and later when you need to display your value you can write a function which displays your displayabletext when you need it.稍后当您需要显示您的值时,您可以编写一个 function ,它会在您需要时显示您的可显示文本。

return map.get("graduated");

The best option is to use a map between livello_studi value and the display value that you want.最好的选择是在livello_studi值和您想要的显示值之间使用 map。

I guess OTHERS is an enum, so first you'll need to define the map data.我猜OTHERS是一个枚举,所以首先你需要定义 map 数据。

levelEducation = new Map([
  ["not-graduated", OTHERS.NotGraduated],
  ["graduated", OTHERS.Graduation],
  ["master", OTHERS.Master],
  ["doctorate", OTHERS.Doctorate],
]);

To get data from the map you do it like this要从 map 获取数据,您可以这样做

levelEducation.get(userData.livello_studi); // this will return undefined if the item is not found

So you could do this in the template to get the data:因此,您可以在模板中执行此操作以获取数据:

<h5>{{levelEducation.get(userData.livello_studi) || '-'}}</h5>

(or move this code into a function for cleanliness) (或将此代码移动到 function 以保持清洁)

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

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