4

我有一个淘汰赛模板,我想在其中定义一个本地参数(它是函数的结果),并在模板中使用它。

<script type="text/html" id="suggestedEmail-template">
    {{ var customVariable = processResult($data); }}
    <li>
        <span data-bind="text: emailValue, attr: { 'data-customValue': customVariable }"></span>
    </li>
</script>

可能吗?

4

2 回答 2

2

您可以执行以下操作:

<script type="text/html" id="suggestedEmail-template">
    <!-- ko if: customVariable = processResult($data) -->
    <li>
        <span data-bind="text: emailValue, attr: { 'data-customValue': customVariable }"></span>
    </li>
    <!-- /ko -->
</script>
于 2016-04-19T18:25:38.560 回答
0

这听起来像是计算变量的工作:

在您的视图模型内部:

var self = this;
self.customVariable = ko.computed(function()
{
    return processResult(this);
};

然后在你的html中:

<span data-bind="text: emailValue, attr: { 'data-customValue': customVariable }"></span>
于 2013-03-04T21:00:51.530 回答