I am trying to use knockout to create a html editor/previewer. I set up a simple test with a single observable as follows:
JS:
var ViewModel = function() {
this.content = ko.observable("<div data-bind=\"text: 'testext'\"></div>");
};
ko.bindingHandlers.bindHTML = {
'init': function () {
},
'update': function (element, valueAccessor) {
ko.utils.setHtml(element, valueAccessor());
}
}
ko.applyBindings(new ViewModel());
HTML:
<input type="text" data-bind="value: content">
This seems to work fine when the page first loads, displaying a div with 'testext' in it, however as soon as I edit the input field to something like
<div data-bind=\"text: 'testext2'\"></div>
the binding doesn't work!
Is this a limitation of knockout, or is there something I'm doing wrong? Is there a way of preforming a rebind?
A JSFiddle is available here: http://jsfiddle.net/Q9LAA/
There is an
html
binding that can insert html for you automatically (I would prefer this over usingsetHtml
), but it doesn't evaluate the bindings on the inserted html, you have to reapply the bindings on the generated html (and that is where you would need a custom binding).Here's an updated fiddle demonstrating its use.