`

angularjs购物车demo

阅读更多
<!DOCTYPE html>
<html ng-app>
 <head>
  <title> New Document </title>
<script type="text/javascript" src="angular.js"></script>

 </head>

 <body ng-controller='CartController'>

 <div ng-repeat='item in items'>
 <span>{{item.title}}</span>
 <input ng-model='item.quantity'/>
<span>{{item.quantity}}</span>
<span>{{item.price | currency}}</span>
<span>{{item.price * item.quantity  | currency}}</span>
<button ng-click="remove($index)">remove</button>
 </div>
 <div>总共:{{totalCart() | currency}}</div>
  <script type="text/javascript">
  function CartController($scope){
  $scope.items=[
  {title:'海尔冰箱',quantity:8,price:3.95},
  {title:'长虹电视',quantity:8,price:3.95},
  {title:'电饭锅',quantity:8,price:3.95}
  ];

  $scope.remove=function(index){
	$scope.items.splice(index,1);
  }

  $scope.totalCart=function(){
  var total = 0;
	for(var i=0;i<$scope.items.length;i++){
	total = total + $scope.items[i].price*$scope.items[i].quantity
	}
  return total;
  };
  }

  </script>
 </body>
</html>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics