1. 程式人生 > >網頁購物車增刪改查小demo(AngularJS)

網頁購物車增刪改查小demo(AngularJS)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/angular.js"></script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.goods=[{
name:"滑鼠",num:1,price:101
},{
name:"鍵盤",num:3,price:601
},{
name:"主機",num:1,price:3001
}];
$scope.delete=function(index){
//刪除指定下標
$scope.goods.splice(index,1);
$scope.allPrice=0;
for(index in $scope.goods){
var zz=$scope.goods[index].num*$scope.goods[index].price;
$scope.allPrice=zz+$scope.allPrice;
}
}
//總金額
$scope.allPrice=0;
for(index in $scope.goods){
var zz=$scope.goods[index].num*$scope.goods[index].price;
$scope.allPrice=zz+$scope.allPrice;
}
//隱藏標籤
$scope.isShow=function(){
if($scope.goods.length > 0){
return true;
}else{
return false;
}
}
});
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<center>
<h3>我的購物車</h3>
<table ng-if="isShow()" border="1px solid black" cellpadding="20" cellspacing="0">
<thead>
<tr>
<th>商品名稱</th>
<th>商品數量</th>
<th>商品單價</th>
<th>商品小計</th>
<th>商品操作</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="good in goods">
<td>{{good.name}}</td>
<td>{{good.num}}</td>
<td>{{good.price | currency:"RMB ¥"}}</td>
<td>{{good.num*good.price | currency:"RMB ¥"}}</td>
<td><button ng-click="delete($index)">刪除</button></td>
</tr>
<tr>
<td colspan="3">總金額</td>
<td>{{allPrice | currency:"RMB ¥"}}</td>
<td></td>
</tr>
</tbody>
</table>
<p ng-if="!isShow()">購物車為空!</p>
</center>
</body>
</html>