淘汰赛无法绑定$ root [英] Knockout UNable to Bind $root
问题描述
我在我的文档中添加了
<div data-bind="text: ko.toJSON($root)"></div>
到我的html5页面.不幸的是IE8给我一个错误无法对$ root应用绑定"或类似的错误.
Inside my Document i've added
<div data-bind="text: ko.toJSON($root)"></div>
to my html5 page. Unfortunately IE8 gives me an Error 'Unable to apply binding to $root' or similar.
在FF和Chrome上没有问题.
No Problems on FF and Chrome.
如何为调试需要恢复当前视图模型的json转储?
How can i retuen a json dump of my current viewmodel for debugging needs?
推荐答案
我能够解决您的问题:问题不是与您的绑定有关,而是与ko.toJSON
调用有关.淘汰赛在内部使用JSON.stringify
函数,在您的情况下会失败.
I was able to repro your issue: the problem isn't with your binding but with the ko.toJSON
call. Knockout uses the JSON.stringify
function internally which fails in your case.
全部 IE 8是否支持JSON.stringify() ? 答案是是.
但是有时它只是不起作用:在Internet Explorer 8中未定义的JSON对象因为IE过于挑剔,如果您的html标记中有错误,它将退回到没有定义JSON.stringify
的兼容模式.
But sometimes it is just not working: JSON object undefined in Internet Explorer 8 because IE is too picky and if you have errors in your html markup it will fall back to compatibility mode where there is no JSON.stringify
definied.
您有两个选择:
- 检查您的HTML以使IE8开心,并且不要在兼容模式下呈现它
- 使用 JSON2 库,该库在较旧的浏览器中提供
JSON.stringify
.
- Check your HTML to make IE8 happy and don't render it in compatibility mode
- Use JSON2 library which provides
JSON.stringify
in older browsers.
这篇关于淘汰赛无法绑定$ root的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!