AngularJs 終極購物車(實例講解)
更新時間:2017年11月08日 08:49:25 作者:六一兒童節(jié)
下面小編就為大家?guī)硪黄狝ngularJs 終極購物車的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
廢話不多說,直接上代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>購物車</title>
<script src="angularjs/angular.js"></script>
<style>
.box{
width: 100%;
border-bottom: 1px solid silver;
}
.box1{
width: 100%;
margin-top: 5px;
}
.box1 button{
width: 100px;
height: 40px;
background: crimson;
color: white;
text-align: center;
line-height: 40px;
float: right;
border: 0;
border-radius: 13px;
}
table{
width: 100%;
}
tr td button{
background: blue;
color: white;
border: 0;
}
</style>
<script>
var my=angular.module("my",[]);
my.controller("mys",function ($scope) {
/*聲明數(shù)據(jù)對象,初始化商品信息,數(shù)據(jù)自擬且不低于四條*/
$scope.arr=[
{name:"qq",price:12.9,number:2,state:false},
{name:"wx",price:23.9,number:1,state:false},
{name:"aa",price:99.9,number:1,state:false},
{name:"bb",price:10.9,number:5,state:false}
];
/*刪除條目*/
$scope.del=function (index) {
if(confirm("確定移除此項嘛?")){
$scope.arr.splice(index,1);
}
}
/*點擊”+”按鈕輸入框中的數(shù)量加1,同時可以計算出商品小計,和購物 車總價*/
$scope.jia=function (index) {
$scope.arr[index].number++;
}
/*當(dāng)點擊”-”按鈕時輸入框中的數(shù)量減1,商品小計和總價*/
$scope.jian=function (index) {
if($scope.arr[index].number>1){
$scope.arr[index].number--;
}
else if($scope.arr[index].number==1){
if(confirm("用戶是否刪除該商品")){
$scope.arr.splice(index,1);
}
}
}
/*計算總價格*/
$scope.allSum=function () {
var allPrice=0;
for(var i=0;i<$scope.arr.length;i++){
allPrice+=$scope.<span style="color:#660e7a"><strong>arr</strong></span>[<span style="color:#458383">i</span>].<span style="color:#660e7a"><strong>price</strong></span>*$scope.arr[i].number;
}
return allPrice;
};
/*清空購物車*/
$scope.alldel=function () {
if($scope.arr.length==0){
alert("您的購物車已空");
}else{
$scope.arr=[];
}
}
/*用戶點擊第一個checkbox代表全選,全選商品后點擊刪除選中商品,選中商品被刪除, 若購物車商品被全部刪除后*/
$scope.allCheck=false;
$scope.allx= function () {
for(var i=0;i<$scope.arr.length;i++){
if($scope.allCheck==true){
$scope.arr[i].state=true;
}else {
$scope.arr[i].state=false;
}
}
};
/*每個復(fù)選框*/
$scope.itemCheck = function () {
var flag = 0;
for(var i = 0; i<$scope.arr.length; i++){
if($scope.arr[i].state == true){
flag ++;
}
}
if(flag == $scope.arr.length){
$scope.allCheck = true;
}else{
$scope.allCheck = false;
}
};
/*批量刪除*/
$scope.pi=function () {
for(var i=0;i<$scope.arr.length;i++){
if($scope.arr[i].state==true){
$scope.arr.splice(i,1);
i--;
$scope.allCheck = false;
}
}
}
});
</script>
</head>
<body ng-app="my" ng-controller="mys">
<div class="box">
<h2>我的購物車</h2>
</div>
<div class="box1">
<button ng-click="alldel()" style="margin-right: 10px">清空購物車</button><button ng-click="pi()" style="margin-left: 5px">批量刪除</button>
</div>
<div class="box2">
<table border="1">
<tr>
<th><input type="checkbox" ng-model="allCheck" ng-click="allx()"/></th>
<th>name</th>
<th>price</th>
<th>number</th>
<th>totalPrice</th>
<th>option</th>
</tr>
<!--用ng-repaet指令將對象遍歷并渲染到頁面中-->
<tr ng-repeat="item in arr">
<td><input type="checkbox" ng-model="item.state" ng-click="itemCheck()"/></td>
<td>{{item.name}}</td>
<td>{{item.price | currency:"¥:"}}</td>
<td><button ng-click="jian($index)">-</button>
<input type="text" value="{{item.number}}" style="width: 30px;padding-left: 20px"/>
<button ng-click="jia($index)">+</button>
</td>
<td>{{item.price*item.number | currency:"¥:"}}</td>
<td><button ng-click="del($index)">刪除</button></td>
</tr>
<tr>
<td colspan="6">總金額<span ng-bind="allSum()|currency:'¥:'"></span></td>
</tr>
</table>
</div>
</body>
</html>
以上這篇AngularJs 終極購物車(實例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS使用ng-app自動加載bootstrap框架問題分析
這篇文章主要介紹了AngularJS使用ng-app自動加載bootstrap框架問題,分析了前面文章中所述的ng-app自動加載bootstrap出現(xiàn)的錯誤原因與相應(yīng)的解決方法,需要的朋友可以參考下2017-01-01
詳解angular路由高亮之RouterLinkActive
這篇文章主要介紹了詳解angular路由高亮之RouterLinkActive,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
對angularjs框架下controller間的傳值方法詳解
今天小編就為大家分享一篇對angularjs框架下controller間的傳值方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
詳解基于Bootstrap+angular的一個豆瓣電影app
本篇文章主要介紹了基于Bootstrap+angular的一個豆瓣電影app ,非常具有實用價值,需要的朋友可以參考下2017-06-06
深究AngularJS——ng-checked(回寫:帶真實案例代碼)
本篇文章主要介紹了深究AngularJS——ng-checked(回寫:帶真實案例代碼),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
AngularJS進(jìn)行性能調(diào)優(yōu)的7個建議
AnglarJS作為一款優(yōu)秀的Web框架,可大大簡化前端開發(fā)的負(fù)擔(dān)。本文給大家介紹AngularJS進(jìn)行性能調(diào)優(yōu)的7個建議,涉及到angularjs性能調(diào)優(yōu)相關(guān)知識,對本文感興趣的朋友一起學(xué)習(xí)吧2015-12-12

