4
我正嘗試在淘汰賽3.2中使用自定義組件。如果我使用預定義的參數,一切都很好。例如,這是jsFiddle。無法通過自定義組件在淘汰賽中傳遞變量3.2
但是,當我從我的視圖模型傳遞參數(我已閱讀如何做到這一點here)我沒有得到任何東西:jsFiddle。我究竟做錯了什麼?
這裏是我的js代碼:
ko.components.register('pagination', {
viewModel: function (params) {
var self = this;
this.page = ko.observable(params.page);
this.max = ko.observable(params.max);
this.list = ko.pureComputed(function() {
var a = self.page(),
list = [],
min = a - 2 < 1 ? 1 : a - 2,
max = a + 2 > self.max() ? self.max() : a + 2;
for (var i = min; i <= max; i++) {
list.push(i);
}
return ko.observableArray(list);
});
this.callback = function (i) {
console.log(i);
self.page(i);
};
},
template: {
element: 'pagination-tpl'
}
});
function Vm(){
this.page = ko.observable(2);
this.max = ko.observable(6);
}
var vm = new Vm();
ko.applyBindings(vm, document.getElementById('koBinding_somePage'));