【问题标题】:Creating a table with nested lists in Angulardart在 Angulardart 中创建带有嵌套列表的表格
【发布时间】:2017-04-07 16:19:35
【问题描述】:

我正在尝试在 Angulardart 中构建一个表示 List<List<int>> 的表。

这是我的模板文件的内容:

<table>
    <tbody>
        <tr *ngFor="#row of cells">
            <td *ngFor="#cell of row">
                <input type="number" #cell (keyup)="0"/>
            </td>
        </tr>
    </tbody>
</table>

并且列表是这样初始化的:

class Grid {
    const GRID_SIZE = 9;
    List<List<int>> cells = new List(GRID_SIZE);

    Grid() {
        cells.forEach((x) => x = new List(GRID_SIZE));
    }
}

但是,在呈现的 HTML 中,我只看到 9 个 &lt;tr&gt; 空标签,里面没有 &lt;td&gt;s。

我错过了什么?

【问题讨论】:

    标签: html dart angular-dart


    【解决方案1】:

    原来问题出在网格初始化中:正确的初始化代码是用零列表填充

    cells.fillRange(0, cells.length, new List.filled(GRID_SIZE, 0));
    

    问题中的初始化代码只用GRID_SIZE空值填充了主列表。

    【讨论】:

      【解决方案2】:

      当您拨打此行时:

      cells.forEach((x) => x = new List(GRID_SIZE));
      

      您实际上是在用一个新值替换 x:您没有干预 x,而是创建了一个新值。

      List<String> myList = ['my', 'original', 'list'];
      
      void yourForLoopFunctionSimulation( List<String> x ) {
        x = new List.filled(GRID_SIZE, 0);
        //you can work with the new x value here, but it won't change the argument that was passed to you
      }
      
      //if you run 
      yourForLoopFunctionSimulation( myList );
      print( myList ); // result : ['my', 'original', 'list'];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-01
        • 1970-01-01
        相关资源
        最近更新 更多