【问题标题】:ngRepeat with Table: Change a cell's display textngRepeat with Table:更改单元格的显示文本
【发布时间】:2014-08-05 20:17:44
【问题描述】:

我有一个要在网格中显示的邮件列表 (<table>)。其中一些邮件带有附件。对于它们对应的行,我想在附件列中显示一个附件图标。休息时,它应该是空的。

JsFiddle:http://jsfiddle.net/6s4Z5/

我的模板如下:

<table id="resultTable">
    <thead>
        <tr>
            <th ng-repeat="columnId in schema.columnOrder">
                <img src="icon_attach.png" ng-if="columnId == 'hasAttachments'">
                {{schema.columns[columnId].displayText}}
            </th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="row in results.mails">
            <td ng-repeat="columnId in schema.columnOrder">
                <img src="icon_attach.png" ng-if="columnId == 'hasAttachments' && row.hasAttachments">
                <span ng-if="columnId != 'hasAttachments'" >{{ row[columnId] }}</span>
            </td>
        </tr>
    </tbody>
</table>

其中schema.columnOrder 是要在表中显示的 columnId 数组。

此模板有效,但这是实现此模板的最佳方式吗?此外,我必须为ng-if 语句添加额外的&lt;span&gt;。也可以去掉吗?

【问题讨论】:

    标签: angularjs angularjs-ng-repeat angular-ng-if


    【解决方案1】:

    您几乎可以将其更改为:

    <span ng-if='your check'><img src=''/>{{row[columnId}}</span>
    

    【讨论】:

    • 对于附件单元格,我不想显示文本(真或假)。对于其他单元格,我想显示文本。
    • 创建一个指令来封装你的显示规则。这样,将来如果您决定以不同的方式显示它,那么您可以对指令进行更改。
    【解决方案2】:

    您可以(在控制器中)将 hasAttachments 列的值设置为您要在此处显示的图像。

    【讨论】:

    • 控制器将hasAttachments 值设置为不同邮件的真/假。这些值可用于其他计算。因此,仅出于显示目的更改其中的值可能不是最佳解决方案。另外,我相信,我需要使用ngSanitize 来添加这样的html。
    • 我将使用一个过滤器进行调查,该过滤器将 true 转换为该列的图标图像。这个方向的东西:github.com/angular/angular-phonecat/blob/master/app/js/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 2012-07-10
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多