Knockout.JS:模型到视图的不同值
我正在使用 KnockoutJS,并且有一个输入控件,可以显示模型中的数字。
一切正常,但是我想在输入控件中显示我的数字之前对其进行格式化 - 但我想将其保留为我的视图中的数字!所以,假设我有以下代码:
我有一个函数 toText () 将数字转换为我需要的数字。但是,这不起作用:
我可以了解问题是 Knockoutjs 不知道如何做相反的事情(从文本转换为数字) - 但这对我来说不是问题,因为我已经以另一种方式处理了。
你能给我推荐一个解决方案吗?
I am using KnockoutJS and I have an input control that displays a number from my model.
Everything works fine, however I'd like to format my number before displaying it in the input control - but I want to keep it as a number to my view! So, let's say that I have the following code:
<td><input class='requiredNum' type='text' data-bind="value: testNum"></input></td>
I have a function toText() that converts the number to what I need. However, this doesn't work:
<td><input class='requiredNum' type='text' data-bind="value: toText(testNum)"></input></td>
I can understand that the problem is that Knockoutjs doesn't know how to do the opposite (convert from text to number) - however this is not a problem to me because I already handle that in another way.
Can you recommend me a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案是使用可写 dependentObservable。
样本:
http://jsfiddle.net/rniemeyer/8bsAF/
The easiest solution is to use a writeable dependentObservable.
Sample:
http://jsfiddle.net/rniemeyer/8bsAF/