AngularJS中的ng-repeat、ng-repeat-start和ng-repeat-end的用法区别详解

    不碰前端好多年,早已成为落伍者;前端技术发展迅速,忙于其他,一直无缘接触AngularJS、VueJS等前端框架。今初尝禁果,额...是第一次亲密接触AngularJS,被其ng-repeat、ng-repeat-start和ng-repeat-end所迷惑,看不错何区别。百度一下,依然没看明白,既然看不明白就自己试验吧。

    代码如下:

<!DOCTYPE html>
<html ng-app="cartApp">
	<head>
		<meta charset="UTF-8">
		<title>购物车</title>
		<script src="../js/angular.min.js"></script>
	</head>
	<body ng-controller="cartController">
		<h1>我的订单:</h1>
		<h1>用ng-repeat-start和ng-repeat-end精确控制循环部分</h1>
		<div>
			<span ng-repeat-start="item in items">{{item.title}}</span>
			<input ng-model="item.quantity"/>
			<span>{{item.price | currency}}</span>
			<span>{{item.price * item.quantity | currency}}</span>
			<button ng-repeat-end="" ng-click="remove($index)">移除</button>				
		</div>
		<h1>改进-用ng-repeat-start和ng-repeat-end精确控制循环部分</h1>
		<div>
			<span ng-repeat-start="item in items">{{item.title}}</span>
			<input ng-model="item.quantity"/>
			<span>{{item.price | currency}}</span>
			<span>{{item.price * item.quantity | currency}}</span>
			<button ng-click="remove($index)">移除</button>
			<br ng-repeat-end=""/>
		</div>
		<h1>不需要精确控制循环体的场景:直接用ng-repeat</h1>
		<div ng-repeat="item in items">
			<span>{{item.title}}</span>
			<input ng-model="item.quantity"/>
			<span>{{item.price | currency}}</span>
			<span>{{item.price * item.quantity | currency}}</span>
			<button ng-click="remove($index)">移除</button>
			<br/>
		</div>
		<script language="JavaScript">
			var app = angular.module("cartApp",[]);
			app.controller('cartController',function($scope)
			{
				$scope.items = [
				{title:'西瓜',quantity:8,price:3.95},
				{title:'桃子',quantity:5,price:2.95},
				{title:'李子',quantity:8,price:3.95}
				];
				$scope.remove = function(index)
				{
					$scope.items.splice(index,1);
				}
			});
		</script>
	</body>
</html>
结论:①当不用精确控制循环体的时候,可以直接使用ng-repeat;

            ②需要精确控制循环体的时候(重点是循环起止位置的时候),可以使用ng-repeat-start,ng-repeat-end.

            ③如果一个div上被设置了ng-repeat-start,那么此div之内(即<div>与</div>)之间是不能放ng-repeart-end的,否则无法运行。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值