【问题标题】:Google Closure Template Bitwise operatorGoogle 闭包模板按位运算符
【发布时间】:2013-10-26 12:47:06
【问题描述】:

我是第一次使用 Google Closure 模板。
我们可以在 Google Closure Template 中使用按位运算符吗?
我想使用这样的东西:

{if $saleStatus.errors & $constant.displayValue}
<div class="displaye">   
<msg desc="user is banned">   
User is Banned.
</msg>   
</div>    
{/if}   

这里我想使用按位运算符,但我抛出语法异常错误。
或者有什么我应该使用的方法。可能是包含js并在那里做点什么?

【问题讨论】:

    标签: javascript google-closure-templates


    【解决方案1】:

    按位 AND 不是 Google Closure 模板中支持的运算符。您应该在调用模板之前在 JavaScript 中对此进行评估,并将其作为参数传递。 See the list of supported operators.

    例如,像这样的东西......

    在 JavaScript 中:

    var err = saleStatus.errors & constant.displayValue;
    $(elem).html(namespace.myTemplate, { err: err });
    

    在大豆/封闭中:

    ....
    
    /**
     * Example ...
     * @param err The error
     */
    {template .myTemplate}
        {if err}
            <div class="displaye">   
                <msg desc="user is banned">   
                    User is Banned.
                </msg>   
            </div> 
        {/if}
    {/template}
    

    有关概念的更多信息,请参阅the documentation.

    【讨论】:

    • 当我只有一次时很好,但是如果我在每个循环中都有这个会发生什么。像 {foreach saleStatus in saleStatuses}。在这种情况下,我有 {saleStatus.errors} 可能每次都有不同的值。
    • 考虑在渲染模板之前计算这些值。您可以通过遍历 saleStatuses 并添加一个包含 saleStatus.errors 和 constant.displayValue 的新属性来做到这一点
    猜你喜欢
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多