敲除数据绑定动态生成的元素 [英] knockout data-bind on dynamically generated elements
本文介绍了敲除数据绑定动态生成的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使knockout数据绑定对动态生成的元素起作用?例如,我在div中插入一个简单的html选择菜单,并希望使用knockout选项绑定填充选项。这就是我的代码:
How is it possible to make knockout data-bind work on dynamically generated elements? For example, I insert a simple html select menu inside a div and want to populate options using the knockout options binding. This is what my code looks like:
$('#menu').html('<select name="list" data-bind="options: listItems"></select>');
但此方法不起作用。任何想法?
but this method doesn't work. Any ideas?
推荐答案
如果在绑定viewmodel后动态添加此元素,它将不在viewmodel中不会更新。你可以做两件事之一。
If you add this element on the fly after you have bound your viewmodel it will not be in the viewmodel and won't update. You can do one of two things.
- 将元素添加到DOM并通过调用
ko.applyBindings(); $重新绑定它c $ c>再次
- 或者从头开始将列表添加到DOM,并将viewmodel中的选项集留空。 Knockout将不会渲染它,直到您稍后向选项添加元素。
这篇关于敲除数据绑定动态生成的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文