【问题标题】:Using a variable in jQuery template with dynamic values在 jQuery 模板中使用具有动态值的变量
【发布时间】:2014-11-07 07:14:25
【问题描述】:

我正在使用 jQuery 模板插件 (jquery.tmpl.min.js - v 1.0.0pre)。我的示例响应对象如下所示:

{
    "statusCode": "SUCCESS",
    "responseMessage": null,
    "response": [
        {
            "id": 92,
            "messageRead": false,
            "importance": null,
            "message": {
                "id": 86,
                "createdDate": 1414393557000,
                "messageType": "TESTTYPE",
                "subject": "test sub",
                "message": "test item",
                "fromOrg": 3,
                "accessCode": 2,
                "fromUserId": 1,
                "users": null,
                "read": false,
                "fromOrganizationName": null,
                "messageAttachment": [
                    {
                        "id": 5,
                        "createdDate": 1414393816000,
                        "name": "README"
                    }
                ]
            }
        }
    ]
}

在我的模板中,在遍历messageAttachment 数组时,我需要在{{each messageAttachment}} 循环内访问fromOrg

{{if message.messageAttachment.length > 0 }}
    {{each message.messageAttachment}}
        \${name} <br/>
        ---- I need value of fromOrg here---
    {{/each}}
{{/if}}

我怎样才能做到这一点?我对此的看法是在循环 messageAttachment 数组之前声明一个变量,并在循环中使用该变量。我提到了这个SO:

Can I declare local/temp variables within a jQuery template?

我尝试使用此${( $data.localVariable = 'SOMETHING' ),''},但无法分配运行时值而不是SOMETTHING

还有,还有其他方法,请分享。任何指针表示赞赏。 TIA。

【问题讨论】:

  • 呃,那只是message.fromOrg,注意它不会在你的循环中改变,因为messageAttachment数组中没有fromOrg
  • 哦,从来没想过尝试一下。它完美地工作。谢谢!
  • @adeneo - 但是,有没有办法将运行时值分配给localVariable?而不是 SOMETHING 在这里完成 - ${( $data.localVariable = 'SOMETHING' ),''} - 就像 ${( $data.localVariable = message.fromOrg ),''}

标签: javascript jquery jquery-templates


【解决方案1】:

正如@adeneo 所建议的,它可以直接作为message.fromOrg 访问,因为messageAttachment 数组中没有fromOrg

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 2021-09-14
    • 1970-01-01
    • 2012-11-08
    • 2017-10-19
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多