If statement 如何编写if/else语句
我必须检查一个变量是否等于某个字符串If statement 如何编写if/else语句,if-statement,knockout.js,conditional-statements,If Statement,Knockout.js,Conditional Statements,我必须检查一个变量是否等于某个字符串 if (product_type == "simple") { ... } 如何在knockout.js中执行此操作?我读过这本书,但不知道该去哪里看 尝试: <!-- ko if: is_product_type && product_type == "simple" --> <div class="primary"> <a data-bin
if (product_type == "simple") { ... }
如何在knockout.js中执行此操作?我读过这本书,但不知道该去哪里看
尝试:
<!-- ko if: is_product_type && product_type == "simple" -->
<div class="primary">
<a data-bind="attr: {href: configure_url, title: $t('foo')}" class="action edit">
<span data-bind="i18n: 'Edit'"></span>
</a>
</div>
<!-- ko else: -->
<div class="primary">
<a data-bind="attr: {href: configure_url, title: $t('bar')}" class="action edit">
<span data-bind="i18n: 'Edit'"></span>
</a>
</div>
<!-- /ko -->
↑ 不起作用,它同时呈现两者。似乎knockout.js甚至不支持
else
,因此您必须创建一个肮脏的解决方法:
<!-- ko if: product_type == "simple" -->
<div class="primary">
<a data-bind="attr: {href: configure_url, title: $t('foo')}" class="action edit">
<span data-bind="i18n: 'Edit'"></span>
</a>
</div>
<!-- /ko -->
<!-- ko if: product_type != "simple" -->
<div class="primary">
<a data-bind="attr: {href: configure_url, title: $t('bar')}" class="action edit">
<span data-bind="i18n: 'Edit'"></span>
</a>
</div>
<!-- /ko -->
另一种选择是使用ifnot绑定。对于更复杂的场景,我通常会将这些类型的决策移动到viewmodel中
<!-- ko if: product_type == "simple" -->
<div class="primary">
<a data-bind="attr: {href: configure_url, title: $t('foo')}" class="action edit">
<span data-bind="i18n: 'Edit'"></span>
</a>
</div>
<!-- /ko -->
<!-- ko ifnot: product_type == "simple" -->
<div class="primary">
<a data-bind="attr: {href: configure_url, title: $t('bar')}" class="action edit">
<span data-bind="i18n: 'Edit'"></span>
</a>
</div>
选择1
选择2
选择3
选择4
我是knockoutjs新手,你能举个例子说明如何将其外包给视图模型吗?