22

我有一个遍历数组(filesObservableArray)的 foreach 循环。该数组有一个键/值,键为:URLPath。当我在 HTML 中绑定数组时,我想用 URLPath 设置“href=”值。

我知道这部分是失败的,但从概念上讲,你能看到我想要做什么吗?

href="<span data-bind='text: URLPath'>"

或者,也许我可以使用 'databind="click: someCode(url)"' 并在 someCode 函数中打开链接?url 映射到文档文件(例如,.doc)或图像文件。

提示赞赏。谢谢!

<tbody data-bind="foreach: $root.filesObservableArray">
                    <tr id="tradeRow">
                        <td><a href="<span data-bind='text: URLPath'></span>">Open file</a></td>
                    </tr>
                </tbody>
4

2 回答 2

43

我不确定你想用 href 中的 span 实现什么,但是通过attr绑定,你可以很好地设置href(或任何其他)属性:

<tbody data-bind="foreach: $root.filesObservableArray">
   <tr id="tradeRow">
       <td><a data-bind="attr: { href: URLPath }">Open file</a></td>
   </tr>
</tbody>
于 2013-05-03T17:26:56.863 回答
7

您是否看过可能使用attr绑定。

http://knockoutjs.com/documentation/attr-binding.html

于 2013-05-03T17:27:15.590 回答