AngularJS入门(用ng-repeat指令实现循环输出
<!doctypehtml>
<htmlng-app>
<head>
<metacharset="utf-8">
<title>ng-repeatdirective</title>
</head>
<body>
<tableng-controller="CartController">
<caption>我的购物车</caption>
<tr>
<th>序号</th>
<th>商品</th>
<th>单价</th>
<th>数量</th>
<th>金额</th>
<th>操作</th>
</tr>
<trng-repeat="iteminitems">
<td>{{$index+1}}</td>
<td>{{item.name}}</td>
<td>{{item.price|currency}}</td>
<td><inputng-model="item.quantity"></td>
<td>{{item.quantity*item.price|currency}}</td>
<td>
<buttonng-click="remove($index)">Remove</button>
</td>
</tr>
</table>
<scriptsrc="../lib/angularjs/1.2.26/angular.min.js"></script>
<script>
functionCartController($scope){
$scope.items=[
{name:"雷柏(Rapoo)V500机械游戏键盘机械黄轴",quantity:1,price:199.00},
{name:"雷柏(Rapoo)V20光学游戏鼠标黑色烈焰版",quantity:1,price:139.00},
{name:"AngularJS权威教程",quantity:2,price:84.20}
];
$scope.remove=function(index){
$scope.items.splice(index,1);
}
}
</script>
</body>
</html>
ng-repeat指令生命在需要循环内容的元素上,items和控制器上的变量名对应,item是为数组中单个对象起的别名。$index可以返回当前引用对象的序号,从0开始,另外还有$first、$middle、$last可以返回布尔值,用于告诉你当前元素是否是集合中的第一个中间的最后一个元素。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短