Knockout:嵌套不识别父模型

时间:2013-09-23 14:24:25

标签: knockout.js

我有这样的事情:

<table data-bind="foreach: tuts" class="tutorsTable">
(...) //here it sees my model
<!-- ko if: date.getTime() > (new Date().getTime() - 900000) -->
    <tr>
        <td colspan="2" class="center">
            <a data-bind="attr: { href: 'https://mypage.com/videoconference/talk/' + peer + '/1/1/0'}, text: 'Start!'"></a>//this is being rendered as is
        </td>
    </tr>
<!-- /ko -->
</table>

为什么它会按原样在if语句中呈现代码,而不是执行data-bind属性呢?

2 个答案:

答案 0 :(得分:0)

您应该在<!-- ko if -->标记

中使用<tr>
<table data-bind="foreach: tuts" class="tutorsTable">
    <tr>
    <!-- ko if: date.getTime() > (new Date().getTime() - 900000) -->
        <td colspan="2" class="center">
            <a data-bind="attr: { href: 'https://mypage.com/videoconference/talk/' + peer + '/1/1/0'}, text: 'Start!'"></a>//this is being rendered as is
        </td>
    <!-- /ko -->
    </tr>
</table>  

否则你会得到

Uncaught Error: Cannot find closing comment tag to match <!- ko...

异常。
如果您的日期为date().getTime(),则还应使用observable JSFiddle DEMO

答案 1 :(得分:0)

问题在于我的日期表示。它是一个简单的字符串,我需要做的是初始化new Date(date.replace(/(\d{2})\.(\d{2})\.(\d{4}) (\d{2}):(\d{2})/,'$3-$2-$1 $4:$5')).getTime()