解决 HTML 上的 Observable 绑定问题
我在 Knock Out 上创建了两个可观察变量,如下所示:
self.winImage1= ko.observable();
self.winImage2= ko.observable();
并为 JS 中的可观察变量验证并存储了基于平台的 CSS,如下所示:
self.findOperatingSystem = function (data) {
if (navigator.appVersion.indexOf("Win") != -1) {
data.winImage1("window_icon_1_css");
data.winImage2("windows_icon_2_css");
}
else if (navigator.appVersion.indexOf("Mac") != -1){
data.winImage1("mac_icon_1_css");
data.winImage2("mac_icon_2_css");
}
}
然后尝试将可观察变量作为类添加到 HTML,如下所示:
<div class="some_class1" data-bind="visible: $root.DeviceModel.findOperatingSystem ($parent)">
<i data-bind="foreach: $data.winImage1()"></i>
<i data-bind="foreach: $data.winImage2()"></i>
</div>
但是使用 foreach 时出现错误。 有人请帮助确定这里做错了什么。 怀疑使用了错误的数据绑定!
请帮我解决这个问题。
I have created two observables variables on Knock Out as follow:
self.winImage1= ko.observable();
self.winImage2= ko.observable();
And validated and stored platform based CSS for the observable variables in JS as follow:
self.findOperatingSystem = function (data) {
if (navigator.appVersion.indexOf("Win") != -1) {
data.winImage1("window_icon_1_css");
data.winImage2("windows_icon_2_css");
}
else if (navigator.appVersion.indexOf("Mac") != -1){
data.winImage1("mac_icon_1_css");
data.winImage2("mac_icon_2_css");
}
}
And then tried to add the observable variables to HTML as a class as follows:
<div class="some_class1" data-bind="visible: $root.DeviceModel.findOperatingSystem ($parent)">
<i data-bind="foreach: $data.winImage1()"></i>
<i data-bind="foreach: $data.winImage2()"></i>
</div>
But getting error while using foreach.
Someone, please help to identify what am doing wrong here.
Suspect using wrong data-bind!
please help me to solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 foreach 不是这样工作的。您将需要一组可观察量来使用它,如下所示:
The problem is that foreach does not work like that. You will need an array of observables to work with it like so: