admin管理员组文章数量:1391955
How do you have conditional binding based on other properties?
Example..
var ViewModel = { IsAdded = ko.observable(), AddedBy = ko.observable() }
When I display it.. I don't want to show AddedBy if IsAddedBy is null or false
Something like this..
<input type="text" data-bind="value: if (IsAdded != null && IsAdded) { AddedBy }"/>
I know that isn't right, but something like that...
How do you have conditional binding based on other properties?
Example..
var ViewModel = { IsAdded = ko.observable(), AddedBy = ko.observable() }
When I display it.. I don't want to show AddedBy if IsAddedBy is null or false
Something like this..
<input type="text" data-bind="value: if (IsAdded != null && IsAdded) { AddedBy }"/>
I know that isn't right, but something like that...
Share Improve this question asked Sep 24, 2012 at 14:14 jaekiejaekie 2,3034 gold badges31 silver badges55 bronze badges 1- 1 Do you want to hide the whole input box, or just not populate it? If you want to hide it entirely, look at the Visible binding. If you don't want to populate it, then Tim's answer is the way to go – James Thorpe Commented Sep 24, 2012 at 14:40
2 Answers
Reset to default 7What I would do is this;
var ViewModel = function() {
this.IsAdded = ko.observable('True');
this.AddedBy = ko.observable('Test');
this.AddedByText = ko.puted(function(){
if ( this.AddedBy() != null && this.IsAdded() ) return this.AddedBy()
return "";
}, this);
}
Then your input would be
<input type="text" data-bind="value: AddedByText" />
This way you are keeping the logic contained within your ViewModel and separate from the HTML.
This question is old but it might help someone else looking
<input type="text" data-bind="value: IsAdded ? AddedBy : "" "/>
Basically if IsAdded is not null then set the value
to AddedBy, else do nothing
本文标签: javascriptKnockoutjs conditional bindingStack Overflow
版权声明:本文标题:javascript - Knockout.js conditional binding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744703894a2620724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论