How to call Knockout’s ko.applyBindings with an element that has been jQuery append the DOM ?

How to call Knockout’s ko.applyBindings with an element that has been jQuery append the DOM ?

<html>
<body>
    <div id='main'>
    </div>
</body>

The following code is

var viewModel = {
    choices: ["one", "two", "three"],
    selected: ko.observable()
};

// this HTML would be loaded from the server
var htmlWithBindings = '<div><b data-bind="text: selected"></b><select data-bind="options: choices, value: selected"></select></div>';

var jQueryObj = $(htmlWithBindings);

ko.applyBindings(viewModel, jQueryObj.get(0));

$('#main').append(jQueryObj);
廣告

About Mr.Brain

Write less code and do more
本篇發表於 JavaScript, Knockout。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s