Knockout.js:在 <select> 上使用无容器 foreach在 Internet Explorer 8 中失败 [英] knockout.js: using a containerless foreach on a <select> fails in Internet Explorer 8
问题描述
在 RP Niemeyer 的文章中 Knockout.js 性能问题 #3 - 所有绑定一起触发,提供了一种构建下拉列表的方法:
此代码也在文章的 jsfiddle 中.
http://jsfiddle.net/rniemeyer/QjVNX/
然而,当我在项目中采用这种构建选择的方法时,它工作得很好,直到我在 Internet Explorer 8 中测试.IE8 失败并且是无法解析绑定".
确实,在 IE8 中运行文章中的 fiddle 也会导致错误.有没有办法在 IE8 中构建 select (我假设 IE8 或更少)使用淘汰赛的 foreach?
是的,这绝对是一个问题.我将不得不更新帖子.IE 将从 select 中删除注释.
这是我们在 github 上看到的一个类似问题:https://github.com/SteveSanderson/knockout/issues/578
一种解决方案是使用 Michael Best 的 repeat
绑定:https://github.com/mbest/knockout-repeat.
它允许您做的一件事是在不同的上下文中多次重复单个元素.因此,在这种情况下,它的行为类似于无容器语法,只是没有注释.
带有重复的示例:http://jsfiddle.net/rniemeyer/QjVNX/>
您也可以选择使用文章中的其他技术 (isolatedOptions
),除非您需要对选项元素进行更多控制.
In RP Niemeyer's article Knockout.js Performance Gotcha #3 - All Bindings Fire Together, an approach to building a dropdown list is provided:
<select data-bind="value: selectedOption">
<!-- ko foreach: options -->
<option data-bind="attr: { value: id }, text: name"></option>
<!-- /ko -->
</select>
This code is also in a jsfiddle from the article.
http://jsfiddle.net/rniemeyer/QjVNX/
However, when I employed this method of building a select in a project, it was working just fine, until I tested in Internet Explorer 8. IE8 failed and was "unable to parse bindings".
Indeed, running the fiddle from the article in IE8 also results in an error. Is there a way to build the select in IE8 (I assume IE8 or less) using knockout's foreach?
Yes, that is definitely an issue. I will have to update the post. IE will strip comments from inside the select.
Here is an issue that we saw on github that is similar: https://github.com/SteveSanderson/knockout/issues/578
One solution is to use Michael Best's repeat
binding: https://github.com/mbest/knockout-repeat.
One of the things that it allows you to do is repeat a single element multiple times with a different context. So, in that case it would act similar to the containerless syntax, just without the comments.
Sample with repeat: http://jsfiddle.net/rniemeyer/QjVNX/
You can also choose to use the other technique from the article (isolatedOptions
), unless you need more control over your option elements.
这篇关于Knockout.js:在 <select> 上使用无容器 foreach在 Internet Explorer 8 中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!