Angular.js中用ng-repeat-start實現自訂顯示_AngularJS

來源:互聯網
上載者:User

前言

眾所周知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能有所協助,如果有疑問大家可以留言交流,謝謝大家對雲棲社區的支援。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.