【问题标题】:ng-repeat on table within ng-repeat div not working as expected on custom filtered datang-repeat div 中的表上的 ng-repeat 在自定义过滤数据上未按预期工作
【发布时间】:2014-06-12 07:46:26
【问题描述】:

以下(所需的)模板无法按预期工作(每年一个 div,每个包含一个每个月的表格 - 传统的年度计划风格日历):

<div ng-repeat="year in calendar | ToCalendar">
  {{year.name}}
  <table ng-repeat="month in year.children">
    <caption>{{month.name}}</caption>
    <tr>
      <th ng-repeat="d in month.children[1].children track by $index">
        {{d}}
      </th>
    </tr>
    <tr ng-repeat="week in month.children">          
      <td ng-repeat="d in week.children track by $index">
        {{d.day.format('DD')}}
      </td>
    </tr>
  </table>
</div>

外部 div 元素在表格之前关闭(至少在 chrome 中),因此表格上的 ng-repeat 没有所需的父范围。所以所有的渲染都是每年的一个 div。

$scope.calendar 是一个 moment.js 对象数组。自定义过滤器“ToCalendar”使用创建附加属性(年、月、工作日和周)允许 _.nest() 按年、月和周嵌套这些属性。为了给出结构的概念,模板中的以下内容:

<pre>{{calendar | ToCalendar | json}}</pre>

以下结果被渲染:

[
  {
    "name": "2014",
    "children": [
      {
        "name": "September",
        "children": [
          {
            "name": "36",
            "children": [
              null,
              {
                "year": 2014,
                "month": "September",
                "week": "36",
                "weekday": "1",
                "day": "2014-08-31T23:00:00.000Z" //Is in fact a moment() object
              }
              ...

如果我更改所需的模板,以便表格及其子元素是具有 css“显示:表格”等的 div,它会根据需要呈现。如果我将外部 div 更改为 span 并使用 ng-repeat-start (以及后面带有 ng-repeat-end 的空元素),但将表格元素保留为真正的表格元素,它就可以工作。这个 hacky 修复可能通过强制表格元素在正确的父范围内来工作。

过滤器“ToCalendar”似乎导致重复的 div 提前关闭,但为什么呢?为什么内部元素是否是div有什么区别?


JS 小提琴:working fiddle。见 cmets。

【问题讨论】:

  • 你能提供一个小提琴吗?
  • @DayanMorenoLeon 一旦 jsfiddle 重新启动并运行,我就试试看。
  • 按预期在 JSFiddle 上工作。不起作用的版本正在作为 couchdb 的 couchapp 提供。我会看看本地版本是否有效。
  • 按预期在本地工作。那么,为什么要把它作为 couchapp 来使用呢?

标签: angularjs couchdb


【解决方案1】:

问题显然出在模板的不同部分,因为剥离回所需的模板修复了它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2015-06-21
    相关资源
    最近更新 更多