【问题标题】:Knockout observablearray thows undefined error淘汰 observablearray 抛出未定义的错误
【发布时间】:2014-01-22 00:54:03
【问题描述】:

将新值推入数组时出现未定义错误。

html:

 <select id="temp" data-bind="options:Original"></select>
 <select data-bind="options:dynamicaarry"></select>

视图模型:

var ViewModal=function(items) {
    this.dynamicaarry=ko.observableArray(items);
    this.Original=ko.observableArray(['volvo','saab','mercedes','audi']);
};

ko.applyBindings(new ViewModal(['four']));
$("#temp").change(function() {
    this.ViewModal.dynamicaarry.push('six'); //throws undefined error});});
}

是否可以在不使用 jquery 的情况下在淘汰赛中处理 selectedindexchange 事件?

【问题讨论】:

标签: jquery knockout.js


【解决方案1】:

您正试图在不同的上下文中调用 ViewModal。因此,首先,您应该从更改事件处理程序中删除this,其次,您不应该使用 jQuery 更改事件处理程序,而应该使用敲除绑定:

HTML:

<select id="temp" data-bind="options:Original, event: { change: changeOriginal }"></select>

视图模型:

var ViewModal=function(items) {
    this.dynamicaarry=ko.observableArray(items);
    this.Original=ko.observableArray(['volvo','saab','mercedes','audi']);

    this.changeOriginal = function() {
        this.ViewModal.dynamicaarry.push('six');
    }
};

ko.applyBindings(new ViewModal(['four']));

【讨论】:

  • 效果很好。
猜你喜欢
  • 2014-02-05
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-28
  • 1970-01-01
相关资源
最近更新 更多