簡體   English   中英

Angular 2如何通過模板表達式中的鍵名訪問數組值

[英]Angular 2 How to access array value by key name in a template expression

我有這個對象我試圖訪問Angular 2模板表達式:

   "purchMgt" : {
        "psas" : [ 
            {
                "psaEffective" : ISODate("2014-09-09T07:00:00.000Z"),
                "psaConsentNote" : "Yes",
            }
        ]
    }

我嘗試了以下的多次迭代:

<p>Title Issue 1 - Consent to Assign Required {{purchMgt?.psas?[0].psaConsentNote || '--'}}</p>

或者像這樣:

<p>Title Issue 1 - Consent to Assign Required {{purchMgt?.psas?[0]?.psaConsentNote || '--'}}</p>

要么:

<p>Title Issue 1 - Consent to Assign Required {{purchMgt?.psas?[psaConsentNote] || '--'}}</p>

並且:

<p>Title Issue 1 - Consent to Assign Required {{purchMgt?.psas?["psaConsentNote"] || '--'}}</p>

這種解決方法有效但如果有更好的方法我會給出接受的答案:

在HTML上

<strong>{{psaParser(project?.realEstate?.purchMgt?.psas, 'psaConsentNote') || '--'}}</strong>

在組件上

public psaParser(psa, key) {

    if (psa) {
        return psa[0][key];

    } else {

        return null;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM