AngularJS ng-repeat遍历输出的用法
更新时间:2017年06月19日 16:23:32 作者:Darkersky
本篇文章主要介绍了AngularJS ng-repeat遍历输出的用法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
AngularJS ng-repeat遍历输出的用法,最近需要用,就顺便发到随笔上了
<!doctype html> <html> <head> <meta charset="utf-8"> <title>ng-repeat directive</title> </head> <body ng-app="myApp"> <table ng-controller="CartController"> <caption>我的购物车</caption> <tr> <th>序号</th> <th>商品</th> <th>单价</th> <th>数量</th> <th>金额</th> <th>操作</th> </tr> <tr ng-repeat="item in items"> <td>{{$index + 1}}</td> <td>{{item.name}}</td> <td>{{item.price | currency}}</td> <td><input ng-model="item.quantity"></td> <td>{{item.quantity * item.price | currency}}</td> <td> <button ng-click="remove($index)">Remove</button> </td> </tr> </table> <script src="js/angular-1.3.0.14/angular.min.js"></script> <script> var app = angular.module('myApp', []); app.controller('CartController',function($scope){ $scope.items = [ {name: "苹果 iPhone7", quantity: 1, price: 5088.00}, {name: "荣耀Magic", quantity: 1, price: 3699.00}, {name: "vivo X9", quantity: 2, price: 2798.00} ]; //$index包含了ng-repeat过程中的循环计数 $scope.remove = function (index) { $scope.items.splice(index, 1); } }) </script> </body> </html>
ng-repeat指令生命在需要循环内容的元素上,items和控制器上的变量名对应,item是为数组中单个对象起的别名。
$index可以返回当前引用对象的序号,从0开始,另外还有$first、$middle、$last可以返回布尔值,用于告诉你
当前元素是否是集合中的第一个中间的最后一个元素。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
对angularJs中ng-style动态改变样式的实例讲解
今天小编就为大家分享一篇对angularJs中ng-style动态改变样式的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-09-09详解JavaScript的AngularJS框架中的表达式与指令
这篇文章主要介绍了JavaScript的AngularJS框架中的表达式与指令,文中罗列了几个常用的指令属性加以说明,需要的朋友可以参考下2016-03-03Angular中使用ng-zorro图标库部分图标不能正常显示问题
这篇文章主要介绍了Angular中使用ng-zorro图标库部分图标不能正常显示问题,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下2019-04-04基于Angularjs+mybatis实现二级评论系统(仿简书)
这篇文章主要为大家详细介绍了基于Angularjs+mybatis实现二级评论系统,模仿简书效果制作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-02-02
最新评论