Can anyone please tell me how to bind data to an iframe
using Knockout? I have tried to do this as below, but it is not working as expected:
<iframe data-bind ="attr: { src: testcontent}"></iframe>
And Javascript:
var ViewModel = function (content) {
this.testcontent = ko.observable(content);
};
ko.applyBindings(new ViewModel("Hello World!!"));
I want to add the text "Hello Content" into the iframe
. Can anyone please help me on this?
Warning: this obviously has security implications! Only do this with code from sources you absolutely trust.
Here's a basic, straightforward solution to build on. It allows you to have an observable with an entire html structure, and fill the iFrame with that data. If you update the html, the iframe is updated with the new version:
You can use this like this in your view:
See this jsfiddle for a demo. The
valueUpdate
is merely there so the demo is clearer, it's debatable if that's a good idea in bigger scenarios.EDIT: Fiddle Updated.
http://jsfiddle.net/sujesharukil/NnT78/10/
You need to create a custom binding handler for this. I have used one such by http://jsfiddle.net/mbest/GYRUX/
and changed it to suit your needs. Take a look at both and see what works for your needs.
You can have the code like this, works absolutely fine :-
OR