我有类似以下内容:
var ChildViewModel = function (viewModel) {
// state
this.viewModel = viewModel;
this.index = ko.dependentObservable(function () {
return this.viewModel.selections().indexOf(this);
}, this);
this.remove = function () {
this.viewModel.removeSelection(this);
};
this.moveUp = function () {
this.move(-1);
};
this.moveDown = function () {
this.move(1);
};
this.move = function (direction) {
var i = this.index();
this.remove();
this.viewModel.selections.splice(i + direction, 0, this);
};
// additional properties
};
var viewModel = {
selections: ko.observableArray(),
removeSelection: function (item) {
this.selections.remove(item);
},
addSelection: function (event) {
var child = new ChildViewModel(this);
this.selections.push(child );
}
};
ko.applyBindings(viewModel);
当我调用 addSelection
时,我在 KnockoutJS 库中收到 Object does not support this property or method
异常。我的应用程序在 Firefox 3.6 和 Chrome 中运行良好。我在 IE8 中遇到异常。我正在使用 KnockoutJS 版本 2.0 1.3 Beta。
我做错了什么?
最佳答案
好吧,我有两个不同的问题。
首先,我删除了获取数组值的方法,而是使用了 observable 数组。
// Bad code for IE8
this.viewModel.selections().indexOf(this);
// this works
this.viewModel.selections.indexOf(this);
此外,我还设置了 for
属性。 IE 提示 for
是一个关键字。
// Bad code for IE8
data-bind="attr: { for : logicalOperatorAndFieldId }"
// Good code for IE8
data-bind="attr: { 'for' : logicalOperatorAndFieldId }"
关于javascript - KnockoutJS 中的 subview 模型在 Internet Explorer 8 中引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8590277/