I constantly find myself using this idiom in KO-based HTML templates:
<!-- ko if: isEdit -->
<td><input type="text" name="email" data-bind="value: email" /></td>
<!-- /ko -->
<!-- ko ifnot: isEdit -->
<td data-bind="text: email"></td>
<!-- /ko -->
Is there a better/cleaner way to do conditionals in KO, or is there a better approach than just using traditional if-else constructs?
Also, I would just like to point out that some versions of Internet Explorer (IE 8/9) don't parse the above example correctly. Please see this SO question for more information. The quick summary is, don't use comments (virtual bindings) inside table tags to support IE. Use the tbody
instead:
<tbody data-bind="if: display"><tr><td>hello</td></tr></tbody>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…