如何为 Knockout.js 同时拥有可见绑定和模板绑定
在 Knockout 中对同一目标进行不同绑定的正确方法是什么?
这些似乎不起作用:
<div data-bind="template: { name: 'voucher-template', foreach: voucher }" data-bind="visible: showVoucher"></div>
或
<div data-bind="template: { name: 'voucher-template', foreach: voucher }, visible: showVoucher"></div>
完整测试代码:
<script>
function VoucherViewModel() {
this.voucher = [
{
VoucherNumber: "100000",
VoucherImage: "someurl",
VoucherExpiry: "20/3/12",
VoucherRedeemed: true,
VoucherDesc: "ddwqdwqdwqd",
VoucherPuchaseDate: "20/12/11",
VoucherPrice: "50"
},
{
VoucherNumber: "200000",
VoucherImage: "someurl",
VoucherExpiry: "20/3/12",
VoucherRedeemed: true,
VoucherDesc: "ddwqdwqdwqd",
VoucherPuchaseDate: "20/12/11",
VoucherPrice: "50"
}]
};
var viewModel = {
showVoucher: ko.observable(true)
};
$(function () {
//VIEWMODEL
ko.applyBindings(viewModel);
//TEMPLATES
ko.applyBindings(new VoucherViewModel());
});
</script>
<div data-bind="template: { name: 'voucher-template', foreach: voucher }, visible: showVoucher"></div>
<script type="text/html" id="voucher-template">
<h3 data-bind="text: VoucherNumber"></h3>
</script>
What is the right way to have different bindings on the same target in Knockout.
These didn't seem to work:
<div data-bind="template: { name: 'voucher-template', foreach: voucher }" data-bind="visible: showVoucher"></div>
or
<div data-bind="template: { name: 'voucher-template', foreach: voucher }, visible: showVoucher"></div>
Full Test code:
<script>
function VoucherViewModel() {
this.voucher = [
{
VoucherNumber: "100000",
VoucherImage: "someurl",
VoucherExpiry: "20/3/12",
VoucherRedeemed: true,
VoucherDesc: "ddwqdwqdwqd",
VoucherPuchaseDate: "20/12/11",
VoucherPrice: "50"
},
{
VoucherNumber: "200000",
VoucherImage: "someurl",
VoucherExpiry: "20/3/12",
VoucherRedeemed: true,
VoucherDesc: "ddwqdwqdwqd",
VoucherPuchaseDate: "20/12/11",
VoucherPrice: "50"
}]
};
var viewModel = {
showVoucher: ko.observable(true)
};
$(function () {
//VIEWMODEL
ko.applyBindings(viewModel);
//TEMPLATES
ko.applyBindings(new VoucherViewModel());
});
</script>
<div data-bind="template: { name: 'voucher-template', foreach: voucher }, visible: showVoucher"></div>
<script type="text/html" id="voucher-template">
<h3 data-bind="text: VoucherNumber"></h3>
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 Knockout 中指定多个绑定,方法是在同一
data-bind
属性中用逗号(本质上是 JavaScript 对象文字)分隔它们。所以,你会想做:You specify multiple bindings in Knockout by separating them with a comma (it is essentially a JavaScript object literal) in the same
data-bind
attribute. So, you will want to do:这是我的建议,据我所知这是最佳实践。这是因为您在函数内的 viewModel 上设置了一个结构,而不是稍后追加,这样更容易阅读。
This is what I would recommend and as far as I understand it is best practise. This is because you set a structure on the viewModel within the function rather than appending later which is easier to read.
似乎双重 ko.applyBindings 引起了问题。
这有效:
如果有更好的方法来写这个,请告诉我。
编辑版本如下,也尝试映射:
Seems like the double ko.applyBindings is causing an issue.
This worked:
If there is a better way to write this, please let me know.
Edited version below, also trying out mapping: