Angular.js中用ng-repeat-start實(shí)現(xiàn)自定義顯示
前言
眾所周知AngularJS 中可以使用 ng-repeat 顯示列表數(shù)據(jù),這對(duì)大家來(lái)說(shuō)應(yīng)該都不陌生了, 用起來(lái)很簡(jiǎn)單, 也很方便, 比如要顯示一個(gè)產(chǎn)品表格, Controller 的 Javascript 代碼如下:
angular.module('app', [])
.controller('MyController', MyController);
MyController.$inject = ['$scope'];
function MyController($scope) {
// 要顯示的產(chǎn)品列表數(shù)據(jù);
$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.'
}
];
}
對(duì)應(yīng)的 HTML 視圖代碼如下:
<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>
運(yùn)行效果圖:

可是如果全部頁(yè)面都是每個(gè)產(chǎn)品占一行來(lái)顯示, 未免太枯燥了, 比如用戶希望這樣子自定義顯示:

每個(gè)產(chǎn)品占表格的兩行, 這樣的效果用 ng-repeat 就沒辦法實(shí)現(xiàn)了。 不過(guò) AngularJS 提供了 ng-repeat-start 和 ng-repeat-end 來(lái)實(shí)現(xiàn)上面的需求, ng-repeat-start 和 ng-repeat-end 的語(yǔ)法如下:
<header ng-repeat-start="item in items"> Header </header> <div class="body"> Body </div> <footer ng-repeat-end> Footer </footer>
假設(shè)提供了 ['A','B'] 兩個(gè)產(chǎn)品, 則生成的 HTML 結(jié)果如下:
<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-start 和 ng-repeat-end 的用法之后, 上面要求的界面就很容易實(shí)現(xiàn)了, 代碼如下:
<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>
總結(jié)
以上就是Angular.js中利用ng-repeat-start實(shí)現(xiàn)自定義顯示的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用Angular.js能有所幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- angularjs在ng-repeat中使用ng-model遇到的問(wèn)題
- Angular ng-repeat 對(duì)象和數(shù)組遍歷實(shí)例
- AngularJS入門(用ng-repeat指令實(shí)現(xiàn)循環(huán)輸出
- AngularJs ng-repeat 嵌套如何獲取外層$index
- Angularjs的ng-repeat中去除重復(fù)數(shù)據(jù)的方法
- AngularJS使用ng-repeat指令實(shí)現(xiàn)下拉框
- AngularJS使用自定義指令替代ng-repeat的方法
- AngularJS基礎(chǔ) ng-repeat 指令簡(jiǎn)單示例
- AngularJS 獲取ng-repeat動(dòng)態(tài)生成的ng-model值實(shí)例詳解
- Angularjs中ng-repeat-start與ng-repeat-end的用法實(shí)例介紹
相關(guān)文章
學(xué)習(xí)AngularJs:Directive指令用法(完整版)
這篇文章主要學(xué)習(xí)AngularJs:Directive指令用法,內(nèi)容很全面,感興趣的小伙伴們可以參考一下2016-04-04
Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解(下)
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07
Angularjs中ng-repeat-start與ng-repeat-end的用法實(shí)例介紹
這篇文章主要給大家介紹了Angularjs中ng-repeat-start與ng-repeat-end的用法,文章開始先進(jìn)行了簡(jiǎn)單的介紹,而后通過(guò)完整的實(shí)例代碼詳細(xì)給大家介紹這兩者的用法,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12
AngularJS ui-router刷新子頁(yè)面路由的方法
這篇文章主要介紹了AngularJS ui-router刷新子頁(yè)面路由的方法,網(wǎng)上雖然有很多種方法,但是都不適合小編,今天小編給大家分享一個(gè)還不錯(cuò)的方法,需要的朋友可以參考下2018-07-07

