admin管理员组文章数量:1319001
This seems to be a simple issue but I can't seem to figure it out
I just need to display the length of a string that is observable. I have tried achieving the result with a koputed()
function as you can see in the code below, but it always returns zero.
Fiddle with an example
Html
<div id="vm">
<h2>The title is: <span data-bind="text: title"></span></h2>
<h2>The length is: <span data-bind="text: title.length"></span></h2>
<h2>Length from puted: <span data-bind="text: titleLength"></span></h2>
<input data-bind="value: title, valueUpdate: 'keyup'"/>
</div>
JavaScript
function VM() {
var self = this;
self.title = ko.observable();
self.titleLength = koputed(function() {
return self.title.length;
});
}
ko.applyBindings(VM(), document.getElementById('vm'));
This seems to be a simple issue but I can't seem to figure it out
I just need to display the length of a string that is observable. I have tried achieving the result with a ko.puted()
function as you can see in the code below, but it always returns zero.
Fiddle with an example
Html
<div id="vm">
<h2>The title is: <span data-bind="text: title"></span></h2>
<h2>The length is: <span data-bind="text: title.length"></span></h2>
<h2>Length from puted: <span data-bind="text: titleLength"></span></h2>
<input data-bind="value: title, valueUpdate: 'keyup'"/>
</div>
JavaScript
function VM() {
var self = this;
self.title = ko.observable();
self.titleLength = ko.puted(function() {
return self.title.length;
});
}
ko.applyBindings(VM(), document.getElementById('vm'));
Share
Improve this question
edited Aug 20, 2014 at 19:57
Weafs.py
23k9 gold badges57 silver badges79 bronze badges
asked Aug 20, 2014 at 19:50
bzliesbzlies
1411 gold badge2 silver badges8 bronze badges
1 Answer
Reset to default 5Your puted version is almost correct. Change it by this
return self.title().length; // <-- Notice () after title
DEMO
本文标签: javascriptKnockout jsGetting string length of observableStack Overflow
版权声明:本文标题:javascript - Knockout js - Getting string length of observable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742054264a2418203.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论