5

我目前正在尝试将表格行<tr>模板传播到<tbody>标签中。这是一个例子:

HTML:

<table>
    <tbody data-bind="template: { name : 'tableTemplate', foreach : tableRow }">
    </tbody>
</table>
<script type="text/html" id="tableTemplate">
    <tr>
        <!-- First Name -->
        <td data-bind="text: firstName"></td>
        <!-- Last Name -->
        <td data-bind="text: lastName"></td>
    </tr>                        
</script>

杜兰达尔JS:

define(function(require) {
    var self = this;
    self.app = require('durandal/app');

    return {
       tableRow: ko.observableArray([
           { firstName: "DemoFirstName" , lastName: "ExampleLastName" },
           { firstName: "ExampleFirstName", lastName: "DemoLastName" }
       ]);

    //viewAttached and other non-applicable console log functions here
    };
});

HTML 中的所有内容都将正确地进行数据绑定,直到它到达表格;之后所有的数据绑定都失效了。

我对 Durandal 很陌生,我边走边学。

4

4 回答 4

8

I ran into the same problem, and I dug up the answer on the Durandal google group. I posted the results here on my question. KO cannot find template with ID

Basically you can't use named/ID'd Knockout templates yet, they're not supported. Support may come soon, according to the Durandal devs on their newsgroup. The workaround is to use either inline, or Durandal's compose functionality.

于 2013-03-28T19:43:37.730 回答
2

可能想尝试这个而不是模板方法:

<table>
  <tbody data-bind="foreach: tableRow">
    <tr>
      <!-- First Name -->
      <td data-bind="text: firstName"></td>
      <!-- Last Name -->
      <td data-bind="text: lastName"></td>
    </tr>                        
  </tbody>
</table>
于 2013-03-24T18:15:43.637 回答
0

你应该试试compose. 文件在这里durandal

于 2015-04-30T17:29:18.733 回答
0

KO 模板似乎不适用于 Durandal - 改用 View Composition(Durandal 的创建者在此处发布关于它的帖子) - https://github.com/BlueSpire/Durandal/pull/50

这里还有一些简介:KO 找不到带有 ID 的模板

于 2013-07-01T10:41:43.260 回答