javascript - knockout html binding with another binding inside - Stack Overflow

I'm using knockout to dynamically loading content into parts of the page, using the HTML binding.t

I'm using knockout to dynamically loading content into parts of the page, using the HTML binding.

the problem is that the html I want to bind has to do call a function onclick and I need the information about the target and the data that knockout easily send.

something like this:

myFunction($parent, $data)

HTML:

<table>
    <tbody data-bind="foreach: rows" >
        <tr>
            <td data-bind="html: rowValue">this will be a link</td>
        </tr>
   </tbody>
</table>

Later I set the value to be a link with a knockout binding inside:

rowValue("<a href='#' data-bind=click:alert('hello')" + result.Data + "</a>");

Please check the fiddle here to see the full working code.

You can see the difference between the 2 lines I wrote, if I do a javascript onclick it works, but obviously ko is missing a late binding.

I've seen many questions about this but can't find one with a definitive answer.

I want to do this with KO, how can this be acplished? with templates maybe?

I'm using knockout to dynamically loading content into parts of the page, using the HTML binding.

the problem is that the html I want to bind has to do call a function onclick and I need the information about the target and the data that knockout easily send.

something like this:

myFunction($parent, $data)

HTML:

<table>
    <tbody data-bind="foreach: rows" >
        <tr>
            <td data-bind="html: rowValue">this will be a link</td>
        </tr>
   </tbody>
</table>

Later I set the value to be a link with a knockout binding inside:

rowValue("<a href='#' data-bind=click:alert('hello')" + result.Data + "</a>");

Please check the fiddle here to see the full working code.

You can see the difference between the 2 lines I wrote, if I do a javascript onclick it works, but obviously ko is missing a late binding.

I've seen many questions about this but can't find one with a definitive answer.

I want to do this with KO, how can this be acplished? with templates maybe?

Share Improve this question edited Jul 8, 2013 at 10:59 Athari 34.3k17 gold badges110 silver badges152 bronze badges asked Jul 8, 2013 at 9:06 Maurizio In denmarkMaurizio In denmark 4,2842 gold badges32 silver badges66 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

KO applies bindings when you call ko.applyBindings. So if you modify the dom after applyBindings has been called. KO won't be aware of the new dom element.

You can use template this way :

<table>
    <tbody data-bind="foreach: sitesTable.rows" >
        <tr data-bind="foreach: row">
            <td data-bind="template: 'myTemplate' "></td>
        </tr>
    </tbody>
</table>

<br/>


<a href="#" onclick="getNewData()"> click here </a>
<script id="myTemplate" type="text/html">
    <a href='#' data-bind="html: cellValue, click: openAlert"> click </a>
</script>

edit by Maurizio. Use this fiddle as the other linkseems to be broken: See fiddle

You can roll your own version of the html: binding that evaluates any bindings in the HTML that you bind... if you get me.

E.g. see https://stackoverflow./a/20821523/188926

Usage:

<td data-bind="bindHTML: rowValue"></td>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745230694a4617664.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信