我有一个简单的应用程序,可以在桌子上显示水果及其颜色。有一个表组件和一个行组件。表格组件的HTML如下所示:
<table class="table table-condensed">
<thead>
<tr>
<th>Fruit</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<row-component fruit="fruit" ng-repeat="fruit in $ctrl.fruits"></row-component>
</tbody>
</table>
行组件的HTML如下所示:
<tr>
<td>{{$ctrl.fruit.name}}</td>
<td>{{$ctrl.fruit.color}}</td>
</tr>
如您所见,表组件在行组件上具有ngRepeat指令。问题在于呈现的标记看起来像这样:
<table-component class="ng-isolate-scope"><!-- ngRepeat: fruit in $ctrl.fruits --><row-component fruit="fruit" ng-repeat="fruit in $ctrl.fruits" class="ng-scope ng-isolate-scope"><tr>
<td class="ng-binding">Apple</td>
<td class="ng-binding">Red</td>
</tr></row-component><!-- end ngRepeat: fruit in $ctrl.fruits --><row-component fruit="fruit" ng-repeat="fruit in $ctrl.fruits" class="ng-scope ng-isolate-scope"><tr>
<td class="ng-binding">Banana</td>
<td class="ng-binding">Yellow</td>
</tr></row-component><!-- end ngRepeat: fruit in $ctrl.fruits --><row-component fruit="fruit" ng-repeat="fruit in $ctrl.fruits" class="ng-scope ng-isolate-scope"><tr>
<td class="ng-binding">Pear</td>
<td class="ng-binding">Green</td>
</tr></row-component><!-- end ngRepeat: fruit in $ctrl.fruits --><table class="table table-condensed">
<thead>
<tr>
<th>Fruit</th>
<th>Color</th>
</tr>
</thead>
<tbody>
</tbody>
</table></table-component>
浏览器不知道如何处理行组件标签,并且表最终看起来像这样:
我需要怎么做才能正确显示行?我有完整的CodePen code here。
答案 0 :(得分:2)
一个简单的解决方案是将您的组件转换为指令并将其作为属性添加到tr元素
<table class="table table-condensed">
<thead>
<tr>
<th>Fruit</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<tr row-component fruit="fruit" ng-repeat="fruit in $ctrl.fruits"></tr>
</tbody>
</table>
注意:我留下了组件的名称,以便在您的示例中更好地理解,最好将其更改为比row-component
更合适的名称,因为它不再是组件。>