【问题标题】:AngularJS - Creating a new row when using ng-repeatAngularJS - 使用 ng-repeat 时创建一个新行
【发布时间】:2017-06-20 23:33:08
【问题描述】:

我正在使用一个简单的 ng-repeat 来生成国家列表及其相应数据。每个列表中都有一个可以展开和折叠的隐藏行。

我正在努力让这个隐藏的行显示在ng-repeat 的每一行的底部。

HTML:

<tr ng-repeat="country in countries">
   <td>{{country.name}}</td>
   <td>{{country.population}}</td>
   <td>{{country.currency}}</td>
   <td>{{country.gpd}}</td>

   <tr>
        <p>Expand/collapse content</p>
   </tr>
</tr>

输出:

France     61.3     EUR    54
Germany    81.5     EUR    82
Spain      12.7     EUR    78
UK         51.3     GBR    64
Expand/collapse content

所需的输出:

France     61.3     EUR    54
Expand/collapse content

Germany    81.5     EUR    82
Expand/collapse content

Spain      12.7     EUR    78
Expand/collapse content

UK         51.3     GBR    64
Expand/collapse content

【问题讨论】:

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


    【解决方案1】:

    您可以使用ng-repeat-startng-repeat-end 如下:

        <tr ng-repeat-start="country in countries">
           <td>{{country.name}}</td>
           <td>{{country.population}}</td>
           <td>{{country.currency}}</td>
           <td>{{country.gpd}}</td>
        </tr>
        <tr ng-repeat-end>
           <td>Expand/collapse content</td>
        </tr>   
    

    工作小提琴:http://jsfiddle.net/nYzjY/372/

    来源:https://docs.angularjs.org/api/ng/directive/ngRepeat

    【讨论】:

    • 谢谢,你有一个工作小提琴,因为这似乎不起作用
    • 谢谢,我正在使用 Angular 的 v1.1.5,ng-repeat-start/end 可以在这个版本上使用吗?
    • 由于 1.1.5 版本 (code.angularjs.org/1.1.5/docs/api/ng.directive:ngRepeat) 的文档中没有提到,我认为这行不通。顺便说一句,我建议您迁移到 1.2 版本,因为 1.1 版本是实验性版本(请参阅docs.angularjs.org/guide/migration
    • 我会迁移,但是我的读取路径和资源现在是独立的库,我宁愿使用 1.1.5 只有 1 个 http 请求
    【解决方案2】:

    您正在重复 tr,然后在重复之后插入另一个 tr。因为您希望两个 tr's 都被重复。你应该把它们嵌套成一个。例如:

    <div  ng-repeat="country in countries">
        <tr>
           <td>{{country.name}}</td>
           <td>{{country.population}}</td>
           <td>{{country.currency}}</td>
           <td>{{country.gpd}}</td>
    
           <tr>
                <p>Expand/collapse content</p>
           </tr>
        </tr>
        </div>
    

    DEMO

    【讨论】:

    • @default - 谢谢,你有一个工作小提琴,因为这似乎不起作用
    • 谢谢,我正在使用 Angular 的 v1.1.5,ng-repeat-start/end 可以在这个版本上使用吗?
    猜你喜欢
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2020-05-24
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多