前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 ng-repeat-start 进行自定义显示

使用 ng-repeat-start 进行自定义显示

作者头像
beginor
发布2020-08-10 14:57:13
6940
发布2020-08-10 14:57:13
举报

使用 ng-repeat-start 进行自定义显示

AngularJS 中使用 ng-repeat 显示列表数据应该都不陌生了, 用起来很简单, 也很方便, 比如要显示一个产品表格, Controller 的 Javascript 代码如下:

代码语言:javascript
复制
angular.module('app', [])
.controller('MyController', MyController);

MyController.$inject = ['$scope'];
function MyController($scope) {
    // 要显示的产品列表数据;
    $scope.products = [
        {
            id: 1,
            name: 'Product 1',
            description: 'Product 1 description.'
        },
        {
            id: 2,
            name: 'Product 3',
            description: 'Product 2 description.'
        },
        {
            id: 3,
            name: 'Product 3',
            description: 'Product 3 description.'
        }
    ];
}

对应的 HTML 视图代码如下:

代码语言:javascript
复制
    <table class="table">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>description</th>
            <th>action</th>
        </tr>
        <tr ng-repeat="p in products">
            <td></td>
            <td></td>
            <td></td>
            <td><a href="#">Buy</a></td>
        </tr>
    </table>

运行效果图:

ng-repeat
ng-repeat

可是如果全部页面都是每个产品占一行来显示, 未免太枯燥了, 比如用户希望这样子自定义显示:

ng-repeat-start
ng-repeat-start

每个产品占表格的两行, 这样的效果用 ng-repeat 就没办法实现了。 不过 AngularJS 提供了 ng-repeat-startng-repeat-end 来实现上面的需求, ng-repeat-startng-repeat-end 的语法如下:

代码语言:javascript
复制
    <header ng-repeat-start="item in items">
      Header 
    </header>
    <div class="body">
      Body 
    </div>
    <footer ng-repeat-end>
      Footer 
    </footer>

假设提供了 ['A','B'] 两个产品, 则生成的 HTML 结果如下:

代码语言:javascript
复制
    <header>
      Header A
    </header>
    <div class="body">
      Body A
    </div>
    <footer>
      Footer A
    </footer>
    <header>
      Header B
    </header>
    <div class="body">
      Body B
    </div>
    <footer>
      Footer B
    </footer>

了解了 ng-repeat-startng-repeat-end 的用法之后, 上面要求的界面就很容易实现了, 代码如下:

代码语言:javascript
复制
    <table class="table table-bordered">
        <tr ng-repeat-start="p in products">
            <td></td>
            <td rowspan="2"><a href="#">Buy</a></td>
        </tr>
        <tr ng-repeat-end>
            <td></td>
        </tr>
    </table>

本文参考资料: https://code.angularjs.org/1.3.15/docs/api/ng/directive/ngRepeat

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用 ng-repeat-start 进行自定义显示
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档