KnockoutJS foreach 循环中的 if 语句
这里我有这段代码:
<tbody data-bind="foreach: entries">
<tr>
<td><i class="icon-file"></i> <a href="#" data-bind="text: name, click: $parent.goToPath"></a></td>
</tr>
</tbody>
我想要这样的东西(它是伪代码):
<tbody data-bind="foreach: entries">
<tr>
<td><i class="{{ if type == 'file' }} icon-file {{/if}}{{else}} icon-folder {{/else}}"></i> <a href="#" data-bind="text: name, click: {{ if type == 'file' }} $parent.showFile {{/if}}{{else}} $parent.goToPath {{/else}}"></a></td>
</tr>
</tbody>
Is it possible to write like this on KnockoutJS?
Here I have this code:
<tbody data-bind="foreach: entries">
<tr>
<td><i class="icon-file"></i> <a href="#" data-bind="text: name, click: $parent.goToPath"></a></td>
</tr>
</tbody>
I would like to have something like this (it's pseudocode):
<tbody data-bind="foreach: entries">
<tr>
<td><i class="{{ if type == 'file' }} icon-file {{/if}}{{else}} icon-folder {{/else}}"></i> <a href="#" data-bind="text: name, click: {{ if type == 'file' }} $parent.showFile {{/if}}{{else}} $parent.goToPath {{/else}}"></a></td>
</tr>
</tbody>
Is it possible to write something like this on KnockoutJS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种选择是执行以下操作:
此处示例:http://jsfiddle.net/rniemeyer/9DHHh/
否则,您可以通过将一些逻辑移动到视图模型中来简化视图,例如:
然后,在视图模型上添加方法以返回适当的值:
示例如下:http://jsfiddle.net/rniemeyer/9DHHh/1/
One option is to do something like:
Sample here: http://jsfiddle.net/rniemeyer/9DHHh/
Otherwise, you can simplify your view by moving some logic into your view model like:
Then, add methods on your view model to return the appropriate value:
Sample here: http://jsfiddle.net/rniemeyer/9DHHh/1/
您可以使用基于注释标签的无容器控制流语法:
You can use the containerless control flow syntax, which is based on comment tags: