我正在做一个网络项目,我们想尝试使用 KnockoutJS。首先,我使用 Chrome 构建我的应用程序(因为官方认为,Knockout 似乎适用于 IE8),但是当我在 IE8 中尝试我的 Web 应用程序时,我遇到了很多异常:
HTML:
<ul class="nav nav-tabs" id="tabs" data-bind="foreach: items">
<li data-bind="'id': 'id', css: { active: 'id' == $parent.selectedTab().id }, 'click': $parent.changeTab">
<a data-toggle="tab" data-bind="text: name, attr: { 'href': 'href' }"></a>
</li>
</ul>
JS:
var tabs = [
new TabViewModel(1, "Tab 1", true),
new TabViewModel(2, "Tab 2", true),
new TabViewModel(3, "Tab 3", false),
];
function TabViewModel(id, name, enabled) {
var self = this;
self.id = ko.observable("test" + id);
self.name = name;
self.paneId = "tab" + id;
self.href = ko.observable("#tab" + id);
self.displayId = "header_tab" + id;
self.enabled = ko.observable(enabled);
ko.bindingHandlers.changeStates = {
init: function (element, valueAccessor) {
var enable = valueAccessor();
if (enable) {
$(element).removeClass("disabled");
} else {
$(element).addClass("disabled");
}
},
update: function (element, valueAccessor) {
var enable = valueAccessor();
if (enable) {
$(element).removeClass("disabled");
} else {
$(element).addClass("disabled");
}
}
}
}
function SurveyViewModel(tabs) {
var self = this;
self.items = ko.observableArray(tabs);
self.selectedTab = ko.observable(self.items()[0]);
self.changeTab = function (tab) {
if (tab.enabled())
self.selectedTab(tab);
return true;
};
}
ko.applyBindings(new SurveyViewModel(tabs));
我有问题:
- 2 行,'changesStates' 抛出异常:
Unable to parse bindings. Message: TypeError: Object expected; Bindings value
- 3 带有 href 属性的亚麻布:
Error: Unable to parse bindings. Message: TypeError: 'href' is undefined
提前致谢