我正在尝试使用 knockoutjs-2.1.0 创建分页,但出现以下错误:
Uncaught TypeError: Object function h(){if(0return i||e(),a.U.La(h),k} has no method 'slice'
我已将问题缩小为:显然 knockout 不喜欢在使用 ko.computed 创建的对象上调用“切片”方法。我的计算类型是这样的:
this.contactsToShow = ko.computed(function() {
// Represents a filtered list of contacts
// i.e., only those matching the "typeToShow" condition
var desiredType = this.typeToShow();
if (desiredType == "all") {
return this.allContacts();
}
return ko.utils.arrayFilter(this.allContacts(), function(aContact) {
return aContact.contactType == desiredType;
});
}, this);
当我在此处设置“showCurrentPage”属性时它会抛出一个错误:
contactListView.showCurrentPage = ko.dependentObservable(function() {
if (this.currentPage() > this.totalPages()) {
this.currentPage(this.totalPages() - 1);
}
var startIndex = this.pageSize() * this.currentPage();
return this.contactsToShow.slice(startIndex, startIndex + this.pageSize());
}, contactListView);
但是,如果我在设置 showCurrentPage(allContacts 数组)时使用原始的 observableArray,它会起作用。
您可以在这里找到 jsfiddle:http://jsfiddle.net/mzalen/S74rJ/12/
我真的很感激任何关于这个问题的建议,因为它让我发疯。
最佳答案
Knockout 的常见错误:this.contactsToShow
在您的示例中成为函数,您必须将其作为函数调用:
return this.contactsToShow().slice(startIndex, startIndex + this.pageSize());
关于javascript - KnockoutJS - 不能将 "Slice"与 ko.computed 对象一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12565373/