admin管理员组文章数量:1336140
I'd like to reset the value of an input field (text=''
) whenever model.id is null.
How to bind the input value to respond to a certain value of an observable object? Something that would look like:
<input type="text" data-bind="text: if (model.value == null) { '' }" />
I'd like to reset the value of an input field (text=''
) whenever model.id is null.
How to bind the input value to respond to a certain value of an observable object? Something that would look like:
<input type="text" data-bind="text: if (model.value == null) { '' }" />
Share
Improve this question
edited Mar 1, 2017 at 10:30
Simon_Weaver
146k92 gold badges680 silver badges715 bronze badges
asked Dec 14, 2012 at 10:24
pistacchiopistacchio
59k110 gold badges287 silver badges434 bronze badges
4 Answers
Reset to default 5You can use ?
operator in data-bind attribute:
<input type="text" data-bind="value: model.id() == null ? 'Default Value' : model.value()" />
In your viewmodel, initiate value of the property as follow :
var model.value = ko.observable('');
In HTML, you don't have to use confitional expression
data-bind="text: model.value"
check these codes
<input type="text" data-bind="value: id() == true? 'Value is Red' : value()" />
function viewModel() {
this.id = ko.observable(true);
this.value = ko.observable("Value is Green");
}
ko.applyBindings(new viewModel());
http://jsfiddle/d4SKr/
The correct answer should be to create a puted observable to get the label.
self.getLabel = ko.pureComputed(function() {
return this.value() === null ? 'Value is red' : value();
});
<input type="text" data-bind="text: getLabel" />
本文标签: javascriptKnockoutjs Set a default text if a binding has a given valueStack Overflow
版权声明:本文标题:javascript - Knockout.js: Set a default text if a binding has a given value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742395814a2466896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论