在应用了 ngFor 的组件中使用时,表行不会中断

use*_*364 1 angular2-template angular

使用 AngularJS 2.3.1,在以下 html 模板代码中:-

<table>
   <tr>
     <th>col 1</th>
     <th>col 2</th>
   </tr>
   <tr>
     <td>sum 1</td>
     <td>sum 2</td>
   </tr>
   <tablerow *ngFor="let i of data" [item]="i"></tablerow>
</table>
Run Code Online (Sandbox Code Playgroud)

其中tablerow组件 html 模板是:-

<tr>
  <td>{{item.col1}}</td>
  <td>{{item.col2}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

当它被渲染时,组件生成的表行不会像普通表行那样被破坏。

为什么会发生这种情况?我需要做什么来解决这个问题?这是一个错误吗?

Gün*_*uer 5

那不应该起作用。

TableRow相反,使用属性选择器创建组件

selector: '[tablerow]'
Run Code Online (Sandbox Code Playgroud)

和一个模板

<td>{{item.col1}}</td>
<td>{{item.col2}}</td>
Run Code Online (Sandbox Code Playgroud)

并像这样使用它

<tr tablerow *ngFor="let i of data" [item]="i"></tr>
Run Code Online (Sandbox Code Playgroud)