Angular.js中用ng-repeat-start實現自定義顯示
前言
眾所周知AngularJS 中可以使用 ng-repeat
顯示列表數據,這對大家來說應該都不陌生了, 用起來很簡單, 也很方便, 比如要顯示一個產品表格, Controller 的 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 視圖代碼如下:
<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 就沒辦法實現了。 不過 AngularJS 提供了 ng-repeat-start
和 ng-repeat-end
來實現上面的需求, ng-repeat-start
和 ng-repeat-end
的語法如下:
<header ng-repeat-start="item in items"> Header </header> <div class="body"> Body </div> <footer ng-repeat-end> Footer </footer>
假設提供了 ['A','B'] 兩個產品, 則生成的 HTML 結果如下:
<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
的用法之后, 上面要求的界面就很容易實現了, 代碼如下:
<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>
總結
以上就是Angular.js中利用ng-repeat-start實現自定義顯示的全部內容,希望本文的內容對大家學習或者使用Angular.js能有所幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- angularjs在ng-repeat中使用ng-model遇到的問題
- Angular ng-repeat 對象和數組遍歷實例
- AngularJS入門(用ng-repeat指令實現循環(huán)輸出
- AngularJs ng-repeat 嵌套如何獲取外層$index
- Angularjs的ng-repeat中去除重復數據的方法
- AngularJS使用ng-repeat指令實現下拉框
- AngularJS使用自定義指令替代ng-repeat的方法
- AngularJS基礎 ng-repeat 指令簡單示例
- AngularJS 獲取ng-repeat動態(tài)生成的ng-model值實例詳解
- Angularjs中ng-repeat-start與ng-repeat-end的用法實例介紹
相關文章
淺析angularJS中的ui-router和ng-grid模塊
下面小編就為大家?guī)硪黄獪\析angularJS中的ui-router和ng-grid模塊。小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05Angularjs 動態(tài)改變title標題(兼容ios)
這篇文章主要介紹了 Angularjs 動態(tài)改變title標題(兼容ios)的相關資料,需要的朋友可以參考下2016-12-12