knockoutJs 两种方式绑定在 foreach 中不起作用
knockoutJs two way binding not working in foreach
我正在尝试在可观察数组的 Foreach 中的 class 属性中执行双向绑定。
在第一次加载时,它绑定成功。但是当我点击每个 LI 时,我想相应地更改 class 属性。
你能帮我看看这里出了什么问题吗?
问候
萨瓦斯
CarTypeModel
appKeyTours.CarType = function (type, count, active) {
'use strict';
this.Type = type;
this.Count = count;
this.Active = ko.observable(active);
};
CarType OBservableArray
carTypes: ko.observableArray([
new appKeyTours.CarType('Full Size', 10, false),
new appKeyTours.CarType('Compact', 20, true),
]),
车型点击事件
clickCarType: function (e) {
e.Active = !e.Active;
},
这是HTML
<ul data-bind="foreach: carTypes" class="check-square filters-option">
<li data-bind="click: $parent.clickCarType, css:{'active':Active()}" >
<a href="#"><!-- ko text: Type --><!-- /ko --><small>(<!-- ko text: Count --><!-- /ko -->)</small></a>
</li>
</ul>
请尝试
clickCarType: function (e) {
e.Active(!e.Active());
}
而不是
clickCarType: function (e) {
e.Active = !e.Active;
}
我正在尝试在可观察数组的 Foreach 中的 class 属性中执行双向绑定。 在第一次加载时,它绑定成功。但是当我点击每个 LI 时,我想相应地更改 class 属性。
你能帮我看看这里出了什么问题吗?
问候 萨瓦斯
CarTypeModel
appKeyTours.CarType = function (type, count, active) {
'use strict';
this.Type = type;
this.Count = count;
this.Active = ko.observable(active);
};
CarType OBservableArray
carTypes: ko.observableArray([
new appKeyTours.CarType('Full Size', 10, false),
new appKeyTours.CarType('Compact', 20, true),
]),
车型点击事件
clickCarType: function (e) {
e.Active = !e.Active;
},
这是HTML
<ul data-bind="foreach: carTypes" class="check-square filters-option">
<li data-bind="click: $parent.clickCarType, css:{'active':Active()}" >
<a href="#"><!-- ko text: Type --><!-- /ko --><small>(<!-- ko text: Count --><!-- /ko -->)</small></a>
</li>
</ul>
请尝试
clickCarType: function (e) {
e.Active(!e.Active());
}
而不是
clickCarType: function (e) {
e.Active = !e.Active;
}