Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the html binding?

Tags:

knockout.js

I encounter a problem when using the html binding in knockoutjs. In the viewModel.maintable() I have:

this.layerDetails = ko.observable("");

In a function that I add some html in the above attribute:

viewModel.maintable().layerDetails(viewModel.maintable().layerDetails() + "<a href='#' data-bind='click:function(){viewModel.refreshPage(true)}'>link</a>");

Also in html part:

<div data-bind='html: layerDetails'>a</div>

However, I can't trigger the refreshPage function. Did I write the wrong code?

like image 614
nelsonwrong Avatar asked Feb 24 '23 18:02

nelsonwrong


1 Answers

The html binding will not hook up any data-bind attributes. You really have a couple of choices to make this work.

1- use the template binding instead of html. Create a template with your content and Knockout will manage adding/removing DOM elements and hooking up the data-binds.

2- call ko.applyBindings(yourViewModel, theNewRootElement) after setting the layerDetails value. So, you would pass in the data that you want bound and the root of any new elements that were created.

like image 102
RP Niemeyer Avatar answered Mar 05 '23 22:03

RP Niemeyer