【问题标题】:Angular2 @ngFor directive and table generation with DartAngular2 @ngFor 指令和使用 Dart 生成表
【发布时间】:2016-05-13 08:41:14
【问题描述】:

我只有一张地图

.dart

Map<String, String> name = {"title":"Countess","first":"Tommy","last":"Clarke","middle":"Kowan","suffix":"Junior"}

我想使用 angular2 @ngFor 指令来构造一个如下所示的表格

我可以使用@ngFor 以常规方式创建表格,其中名称列形成表格标题行,输入列放置为第一个表格行。显示的图形是使用以下代码完成的:

.html

<table class = "mdl-data-table mdl-js-data-tablemdl-shadow--4dp">
  <thead>
    <tr>
      <th class = "mdl-data-table__cell--non-numeric">Name</th>
      <th>Input</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class = "mdl-data-table__cell--non-numeric">Title</td>
      <td>{{name.title}}</td>
    </tr>
    <tr>
      <td class = "mdl-data-table__cell--non-numeric">First</td>
      <td>{{name.first}}</td>
    </tr>
    <tr>
      <td class = "mdl-data-table__cell--non-numeric">Middle</td>
      <td>{{name.middle}}</td>
    </tr>
    <tr>
      <td class = "mdl-data-table__cell--non-numeric">Last</td>
      <td>{{name.last}}</td>
    </tr>
    <tr>
      <td class = "mdl-data-table__cell--non-numeric">Suffix</td>
      <td>{{name.suffix}}</td>
    </tr>
  </tbody>
</table>

如何使用@ngFor 指令在图形中创建表格?

EDIT1 - 使用第一个解决方案运行时出现控制台错误

>EXCEPTION: Class '_CompactIterable' has no instance method '[]'.

NoSuchMethodError: method not found: '[]'
Receiver: Instance of '_CompactIterable'
Arguments: [null] in [{{names.keys[idx].slice(1,1).toUpperCase()}}{{names.keys[idx].slice(2)}} in NameComponent@148:52]
  EXCEPTION: Class '_CompactIterable' has no instance method '[]'.

NoSuchMethodError: method not found: '[]'
Receiver: Instance of '_CompactIterable'
Arguments: [null] in [{{names.keys[idx].slice(1,1).toUpperCase()}}{{names.keys[idx].slice(2)}} in NameComponent@148:52]
    (anonymous function)    
  ORIGINAL EXCEPTION: Class '_CompactIterable' has no instance method '[]'.

.dart(Dart 的 SnareChops 端口答案如下)|正确答案

添加了一个自定义管道来执行大写

///Takes the first letter of [String] key in a [Map], capitalizes it and 
///returns the key value as the capitallized word
@Pipe( name: 'capitalize' )
class CapitalizePipe extends PipeTransform {
  dynamic transform( String value, List args ) {
    StringBuffer buffer = new StringBuffer( );

    List<String> list = value.split( '' );
    if ( value != null ) {
      for ( int i = 0; i < list.length; i++ ) {
        if ( i == 0 ) {
          buffer.write( list[i].toUpperCase( ) );
          continue;
        }

        buffer.write( list[i] );
      }
    }
    return buffer.toString( );
  }
}

  <tbody>
  <tr *ngFor = "#name of names?.keys">
    <td class = "mdl-data-table__cell--non-numeric">{{name | capitalize}}</td>
    <td>
      {{names[name]}}
    </td>
  </tr>
  </tbody>

【问题讨论】:

  • 用 dart 标签标记帖子可以让用户知道您使用 angular2 的语言,因为它支持 3 种不同的语言,并且每种语言都有其细微差别。
  • @SnareChops 感谢您提供信息。

标签: dart angular angular2-forms


【解决方案1】:

迭代Map 的键,将它们用于标题,然后您可以使用该键访问Map 中的值,从而为您提供值。

<tbody>
  <tr *ngFor="#name of names?.keys">
    <td class="mdl-data-table__cell--non-numeric">{{name}}</td>
    <td>{{names[name]}}</td>
  </tr>
</tbody>

【讨论】:

  • 尝试解决方案时,请查看上述格式化问题中的控制台错误。
  • 非常抱歉,我对飞镖不太了解,这是有效的 JS 和 TS。如何使用 dart 通过索引获取键?或者是names.keys()[idx]...
  • 别着急,原理是对的。请参阅上述问题中更正后的 Dart 端口。在这种情况下,Dart 中不需要 $index。我必须在 Dart 中找到 slice() 方法才能申请大写。如果没有等价物,我将不得不创建一个进行大写的管道。再次感谢。
  • 啊,你是对的,这是实现这一目标的更好方法,也是有效的 JS/TS。既然它确实回答了这个问题,您愿意将其发布为答案吗?
  • 这是你的想法 SnareChops - 用上面的替换你的答案,我将删除我的。这是公平的,也是唯一要做的事情。访问compariative methods of TS, JS and Dart查看类似方法。
猜你喜欢
  • 2017-06-02
  • 2019-05-22
  • 2016-10-11
  • 2016-09-24
  • 2016-04-06
  • 1970-01-01
  • 2016-05-23
  • 2023-03-09
相关资源
最近更新 更多