【问题标题】:ng-switch not working with nested ng-repeat for tableng-switch 不适用于表的嵌套 ng-repeat
【发布时间】: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


【解决方案1】:

ng-switch-when 指令应应用于应用 ng-switch 指令的元素的子元素。在您的情况下,您将它们应用于相同的元素。

尝试(在 tbody 内)

<tr ng-repeat="record in data | orderBy : sortBy : reverse">
    <td ng-repeat="col in metaData.columns">
        <div ng-switch on="col.type">
            <div ng-switch-when="index"> 
                {{record.$index + 1}}
            </div>
            <div ng-switch-when="name"> 
                {{record.name = [record.fname, record.mname, record.lname].join(' ') }}
            </div>
            <div ng-switch-when="date"> 
                {{record[col.dataKey] = toDate(record[col.dataKey]) | date : 'mediumDate'}}
            </div>
            <div ng-switch-when="inMobile"> 
                <a href="tel:{{record[col.dataKey]}}">{{record[col.dataKey]}}</a>
            </div>
            <div ng-switch-when="email"> 
                <a href="mailto:{{record[col.dataKey]}}">{{record[col.dataKey]}}</a>
            </div>
            <div ng-switch-when="gender"> 
                {{record[col.dataKey] == 'm' ? 'Male' : 'Female'}}
            </div>
            <div ng-switch-when="place"> 
                {{record[col.dataKey].name}}
            </div>
            <div ng-switch-default> 
                {{record[col.dataKey]}}
            </div>
        </div>
    </td>
</tr>

【讨论】:

  • 感谢您的回答。我想避免额外的标记。
  • 哪些附加标记? 2 层 div 元素?注意 ngSwitchWhen 必须是 ngSwitch 的子级,所以至少需要添加一层。也许将 ngSwitch 作为 td 的属性会起作用。
  • ng-switch 作为属性不适用于我的用例。我正在研究另一种解决方法,并很快为这种情况发布答案。感谢您的宝贵时间。
猜你喜欢
  • 2018-11-05
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
相关资源
最近更新 更多