在 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>