我已经为 knockout View 模型创建了一个分页器模板
<script type="text/html" id="custom_pager_template">
<ul class="pagination">
<li class="disabled"><a href="#">«</a></li>
<!-- ko foreach: ko.utils.range(0, maxPageIndex) -->\
<li class="active"><a href="#" data-bind="text: $data + 1, click function() { $root.currentpageindex($data) }, css { selected $data= =$root.currentPageIndex() }"><span class="sr-only"></span></a></li>
<!-- /ko -->
</ul>
</script>
但是当我运行这个时,我收到以下错误:
Uncaught SyntaxError: Unable to process binding "simpleGrid: function (){return gridViewModel }" Message: Unable to process binding "foreach: function (){return ko.utils.range(0,maxPageIndex) }" Message: Unable to parse bindings. Bindings value: text: $data + 1, click function() { $root.currentpageindex($data) }, css { selected $data= =$root.currentPageIndex() } Message: Unexpected token )
那么我如何在模板中使用 ko 函数?
最佳答案
您缺少两个三个冒号
前 2 个在这里
css { selected $data= =$root.currentPageIndex() }
应该是
css: { selected: $data == $root.currentPageIndex() }
最后一个在这里:
click function() { ... }
应该是
click: function() { ... }
关于javascript - 如何在模板内使用 ko.utils.range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21530032/