使用无容器控制流时的 Knockout.js 1.3.0 IF 语句语法
以下是 Steve Sanderson 博客中的示例,演示了淘汰赛中的无容器 IF 语句:
<h3>Products</h3>
<ul>
<li><strong>Here is a static header item</strong></li>
<!-- ko foreach: products -->
<li>
<em data-bind="text: name"></em>
<!-- ko if: manufacturer -->
— made by <span data-bind="text: manufacturer.company"></span>
<!-- /ko -->
</li>
<!-- /ko -->
</ul>
我如何使 IF 语句更复杂。我正在尝试以下操作,但它不起作用(总是返回 false):
<!-- ko if: PlanStateName == 'Draft' -->
<div>This plan is a draft!</div>
<!-- /ko -->
如何实现这一目标?
Here's example from Steve Sanderson's blog demonstrating a containerless IF statement in knockout:
<h3>Products</h3>
<ul>
<li><strong>Here is a static header item</strong></li>
<!-- ko foreach: products -->
<li>
<em data-bind="text: name"></em>
<!-- ko if: manufacturer -->
— made by <span data-bind="text: manufacturer.company"></span>
<!-- /ko -->
</li>
<!-- /ko -->
</ul>
How would I make the IF statement more complex. I am trying the following and it doesn't work (always returns false):
<!-- ko if: PlanStateName == 'Draft' -->
<div>This plan is a draft!</div>
<!-- /ko -->
How would one accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明我犯了一个菜鸟错误。这是工作代码:
由于变量是通过剔除进行包装的,因此需要 PlanStateName 上的括号来访问基础数据。
So it turns out I made a rookie mistake. Here's the working code:
Since the variables are wrappered by knockout, the parentheses on PlanStateName are required to access the underlying data.
您需要用 {} 将逻辑语句括起来。
请参阅http://jsfiddle.net/photo_tom/nvYdf/55/
You need to surround the logical statement with {}.
See http://jsfiddle.net/photo_tom/nvYdf/55/