简体   繁体   English

Ngx-translate:如何使用键引用直接在 Angular 中访问 JSON 数组值

[英]Ngx-translate: How to access JSON array value directly in Angular with key reference

I am doing POC on translating my app using ngx-translate.我正在使用 ngx-translate 翻译我的应用程序进行 POC。 I will get JSON response from my API url.我将从我的 API url 获得 JSON 响应。 In that response, can some one help me how to access the JSON array value with out referring array numbers?在该响应中,有人可以帮助我如何在不引用数组编号的情况下访问 JSON 数组值吗? My JSON response is given below.我的 JSON 响应如下。

{
    "Data": {
        "FirstData": [{
                "key": "FirstKey",
                "value": "FirstValue"
            },
            {
                "key": "SecondKey",
                "value": "SecondValue"
            }
        ]
    },
    "IsSuccessful": true,
    "HttpStatusCode": 200,
    "Exception": null
}

So, in my view to refer "FirstValue" , I have to do something like ( which I dont want to)因此,在我看来,要引用"FirstValue" ,我必须做类似的事情(我不想这样做)

<h1> {{'Data.FirstData.0.value' | translate }} </h1> <!--First Value -->

Here "0" is tightly coupled.这里“0”是紧密耦合的。 Is there any other way by accessing via key?通过密钥访问还有其他方法吗?

Is that possible?那可能吗?

Change the response of your API to contain only the key/value pairs.将 API 的响应更改为仅包含键/值对。

This is the desired JSON:这是所需的 JSON:

{
    "FirstKey": "FirstValue",
    "SecondKey": "SecondValue"
}

Then use it like that:然后像这样使用它:

<h1> {{'FirstKey' | translate }} </h1>

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

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