angularjs實(shí)現(xiàn)簡(jiǎn)單的購物車功能
本文實(shí)例為大家分享了angularjs實(shí)現(xiàn)購物車功能的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
margin-left: 10px;
}
li{
list-style: none;
}
.add,.reduce{
display: inline-block;
width: 20px;
height: 20px;
border: 1px solid #000;
text-align: center;
}
.line{
border-bottom: 1px solid #000;
}
</style>
<script src="angular-1.5.5/angular.min.js"></script>
<script>
var myapp=angular.module("myapp",[]);
myapp.controller("myCtrl",function($scope){
$scope.cart=[
{
"shopName":"趣藝工坊",
"checked":false,
"goods":[
{
"goodsName":"純手工制作木質(zhì)時(shí)鐘精致家具裝飾擺件",
"pic":"images/cart_01.jpg",
"price":150.00,
"number":1,
"checked":false
},
{
"goodsName":"木質(zhì)藍(lán)牙音箱包郵實(shí)木家具裝飾擺件",
"pic":"images/cart_02.jpg",
"price":119.00,
"number":2,
"checked":true
},
{
"goodsName":"裝飾木雕,獨(dú)特趣味設(shè)計(jì)家具裝飾擺件",
"pic":"images/cart_03.jpg",
"price":120.00,
"number":0,
"checked":false
}
]
},
{
"shopName":"鄰街紙藝",
"checked":false,
"goods":[
{
"goodsName":"純手工制作木質(zhì)時(shí)鐘精致家具裝飾擺件",
"pic":"images/cart_04.jpg",
"price":89.00,
"number":2,
"checked":true
},
{
"goodsName":"木質(zhì)藍(lán)牙音箱包郵實(shí)木家具裝飾擺件",
"pic":"images/cart_05.jpg",
"price":189.00,
"number":1,
"checked":false
}
]
},
{
"shopName":"紙來我往",
"checked":true,
"goods":[
{
"goodsName":"純手工制作木質(zhì)時(shí)鐘精致家具裝飾擺件",
"pic":"images/cart_06.jpg",
"price":289.00,
"number":3,
"checked":true
}
]
}
];
//點(diǎn)擊加減按鈕,數(shù)量加減;點(diǎn)擊刪除按鈕,刪除商品
$scope.reduce=function(goods){
goods.number--;
if (goods.number<=0) goods.number=0;
$scope.totalMoney();
};
$scope.add=function(kind){
kind.number++;
$scope.totalMoney();
};
$scope.delete=function(item,index){
item.goods.splice(index,1)
};
/*總金額計(jì)算*/
$scope.totalMoney=function(){
var total=0;
for(var i=0;i<$scope.cart.length;i++){
for(var j=0;j<$scope.cart[i].goods.length;j++){
if($scope.cart[i].goods[j].checked){
total+=$scope.cart[i].goods[j].price*$scope.cart[i].goods[j].number;
}
}
}
return total;
};
/*商鋪選擇*/
$scope.shopChecked=function(item){
if(item.checked==true){
for(var i=0;i<item.goods.length;i++){
item.goods[i].checked=true
}
}else {
for(var i=0;i<item.goods.length;i++){
item.goods[i].checked=false;
}
}
};
/*全部選擇*/
$scope.allChecked=function(){
if($scope.allCheck){
for(var i=0;i<$scope.cart.length;i++){
$scope.cart[i].checked=true;
for(var j=0;j<$scope.cart[i].goods.length;j++){
$scope.cart[i].goods[j].checked=true;
}
}
}else {
for(var i=0;i<$scope.cart.length;i++){
$scope.cart[i].checked=false;
for(var j=0;j<$scope.cart[i].goods.length;j++){
$scope.cart[i].goods[j].checked=false;
}
}
}
};
})
</script>
</head>
<body ng-app="myapp" ng-controller="myCtrl">
<div><input type="checkbox" ng-model="allCheck" ng-click="allChecked()">總金額:<span>{{ totalMoney() | currency:"¥"}}</span></div>
<div ng-repeat="item in cart" class="line">
<div><input type="checkbox" ng-model="item.checked" ng-click="shopChecked(item)"><span>{{item.shopName}}</span></div>
<ul>
<li ng-repeat="kind in item.goods">
<input type="checkbox" ng-model="kind.checked"><span>{{kind.goodsName}}</span>
<p>{{kind.price}}</p>
<p><span class="add" ng-click="add(kind)">+</span>{{kind.number}}<span class="reduce" ng-click="reduce(kind)">-</span></p>
<p ng-click="delete(item,$index)">刪除</p>
</li>
</ul>
</div>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Angularjs和bootstrap實(shí)現(xiàn)購物車功能
- 使用Angular.js實(shí)現(xiàn)簡(jiǎn)單的購物車功能
- Angularjs 制作購物車功能實(shí)例代碼
- Angular實(shí)現(xiàn)購物車計(jì)算示例代碼
- angular和BootStrap3實(shí)現(xiàn)購物車功能
- AngularJS 購物車全選/取消全選功能的實(shí)現(xiàn)方法
- angular.js實(shí)現(xiàn)購物車功能
- AngularJS 實(shí)現(xiàn)購物車全選反選功能
- AngularJs 終極購物車(實(shí)例講解)
- Angular動(dòng)畫實(shí)現(xiàn)的2種方式以及添加購物車動(dòng)畫實(shí)例代碼
相關(guān)文章
利用Angular2的Observables實(shí)現(xiàn)交互控制的方法
這篇文章主要介紹了利用Angular2的Observables實(shí)現(xiàn)交互控制的方法,我們主要針對(duì)一些在跟服務(wù)器端交互的時(shí)候遇到的問題,來看看Observable給我們帶來的特性。感興趣的可以了解一下2018-12-12
在 Angular-cli 中使用 simple-mock 實(shí)現(xiàn)前端開發(fā) API Mock 接口數(shù)據(jù)模擬功能的方法
這篇文章主要介紹了在 Angular-cli 中使用 simple-mock 實(shí)現(xiàn)前端開發(fā) API Mock 接口數(shù)據(jù)模擬功能的方法,需要的朋友可以參考下2018-11-11
angular使用md5,CryptoJS des加密的方法
這篇文章主要介紹了angular使用md5,CryptoJS des加密的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
AngularJs導(dǎo)出數(shù)據(jù)到Excel的示例代碼
本篇文章主要介紹了AngularJs導(dǎo)出Excel的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
angularjs http與后臺(tái)交互的實(shí)現(xiàn)示例
這篇文章主要介紹了angularjs http與后臺(tái)交互的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12

