【问题标题】:Two table cells instead of one angularjs两个表格单元格而不是一个 angularjs
【发布时间】:2017-06-20 05:08:24
【问题描述】:

我会尽量简单地解释我的问题。我有这样的事情:

<table>
    <tr>
      <th ng-repeat="ordinate in ordinatesX">{{ordinate.ordinateLabel}}</th>
    </tr>   
</table>

这只是我桌子上的一小部分,但它是有问题的。我的纵坐标对象有一个属性abstract。当 abstract 设置为 true 和 DOM 以制作两个 th 时:

<th>{{ordinate.ordinateLabel}}</th><th></th>

所以第一个th 是写纵坐标标签,第二个th 需要为空。 但是当abstract 设置为false 我只需要一个th 像这样:

<th>{{ordinate.ordinateLabel}}</th>

我已经尝试过像这样最简单的自定义指令:

.directive('ngTest', function() {
        return {
            restrict: 'A',
            replace: true,
            template: '<th></th><th></th>'
        }
});

但由于replace: true 和根元素tr 的问题,即使这样也不起作用。 有什么想法吗?

【问题讨论】:

    标签: angularjs angularjs-directive html-table angularjs-ng-repeat


    【解决方案1】:

    你可以结合ng-repeat-start和ng-if

    ng-if 不会在 DOM 中插入元素。

    <table>
        <tr>
          <th ng-repeat-start="ordinate in ordinatesX">{{ordinate.ordinateLabel}}</th><th ng-if="ordinate.abstract" ng-repeat-end></th>
        </tr>   
    </table>
    

    【讨论】:

    • 完美运行。谢谢你!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多