问题
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.
No Problems on FF and Chrome.
How can i retuen a json dump of my current viewmodel for debugging needs?
回答1:
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.
Altough Is JSON.stringify() supported by IE 8? where the answer is yes.
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.
You have two options:
- 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.
来源:https://stackoverflow.com/questions/14382467/knockout-unable-to-bind-root