使用 applyBindingsToNode 动态绑定元素不响应更改 [英] Dynamically bound elements with applyBindingsToNode not responding to changes

查看:18
本文介绍了使用 applyBindingsToNode 动态绑定元素不响应更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个例子中,我有一个动态绑定的输入和 div 到同一个属性.但是在更改输入中的文本时,更改不会反映在 div 元素中.

In this example I have a dynamically bound input and div to the same property. But on altering text in input, changes are not reflected in the div element.

http://jsfiddle.net/rpuri/Bcps5/

ko.applyBindingsToNode(document.getElementById('input-health'), {
    value: vm.status(),
    valueUpdate: 'afterkeydown'
});

声明式绑定不适合我,因为我需要绑定到部分视图 (ASP.NET MVC) 中的共享元素.

Declarative binding is not an option for me because I need to bind to shared elements in partial views (ASP.NET MVC).

谢谢

推荐答案

您绑定的是 observable 的值,而不是 observable 本身.

You are binding to the value of the observable instead of the observable itself.

试试:

ko.applyBindingsToNode(document.getElementById('health'), {
    text: vm.status, // <- not invoking status, binding to the observable itself.
    valueUpdate: 'keydown'
});

ko.applyBindingsToNode(document.getElementById('input-health'), {
    value: vm.status,  
    valueUpdate: 'keydown'
});

http://jsfiddle.net/hwQsm/

这篇关于使用 applyBindingsToNode 动态绑定元素不响应更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆