【发布时间】:2016-01-22 09:31:33
【问题描述】:
使用嵌套的“ng-repeat”和ng-switch 指令根据提供的列定义和行数据(2 个不同的 JSON 对象)呈现表格时出现错误:
有什么方法可以在不使用包装元素的情况下使用“ng-repeat”和ng-switch 来呈现表格单元格。
Error: [$compile:ctreq] Controller 'ngSwitch', required by directive 'ngSwitchWhen', can't be found!
这是我的表格模板
<table id={{metaData.id}} name="{{metaData.name}}" class="{{metaData.classes}}">
<tbody>
<tr ng-repeat="record in data">
<td ng-repeat="col in metaData.columns" ng-switch on="col.type"
ng-switch-when="index"> {{record.$index + 1}} </td>
</tr>
</tbody>
暂时解决我的问题
我正在使用ng-if 以下列方式动态呈现表cell 和rows:
<table id={{metaData.id}} name="{{metaData.name}}" class="{{metaData.classes}} vk-table" >
<tbody>
<tr ng-repeat="record in data | orderBy : sortBy : reverse">
<td ng-repeat-start="col in metaData.columns" ng-if="col.type == 'index'"> {{$parent.$parent.$index + 1}}. </td>
<td ng-if="col.type =='name'">{{record.name = [record.fname, record.mname, record.lname].join(' ') }}</td>
<td ng-if="col.type =='date'">{{record[col.dataKey] = toDate(record[col.dataKey]) | date : 'mediumDate'}}</td>
<td ng-if="col.type =='inMobile'">
<a href="tel:{{record[col.dataKey]}}">{{record[col.dataKey]}}</a>
</td>
<td ng-if="col.type =='email'">
<a href="mailto:{{record[col.dataKey]}}">{{record[col.dataKey]}}</a>
</td>
<td ng-if="col.type =='gender'">{{record[col.dataKey] == 'm' ? 'Male' : 'Female'}}</td>
<td ng-if="col.type =='place'">{{record[col.dataKey].name}}</td>
<td ng-repeat-end ng-if="!col.type">{{record[col.dataKey]}}</td>
</tr>
</tbody>
</table>
如何在不使用其他元素的情况下使用 ng-switch 实现相同的输出?
【问题讨论】:
-
我只是不想使用额外的包装元素。
-
目前还不清楚您到底想达到什么目的。你为什么首先使用 ng-switch-when?
-
@GiladBeeri :我更新了 暂时解决的问题部分下的问题。希望现在很清楚。
-
查看我修改后的答案@Vikram
标签: angularjs templates angularjs-ng-repeat ng-switch