angular和BootStrap3實(shí)現(xiàn)購(gòu)物車功能
一、頁(yè)面搭建
1、html結(jié)構(gòu)
采用BootStrap3來快速創(chuàng)建一個(gè)購(gòu)物車表單樣式。
主要功能:
A:列表數(shù)據(jù)所示
B:增加刪除端口
C:清空購(gòu)物車
D:商品數(shù)量的增減
E:動(dòng)態(tài)計(jì)算商品價(jià)格以及總價(jià)
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
<meta charset="UTF-8">
<title>購(gòu)物車</title>
<link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css">
<script src="../vendor/angular.js"></script>
<script src="car-controller.js"></script>
</head>
<body>
<div class="table-responsive container" ng-controller="CarCtrl">
<table class="table " ng-show="data.length">
<thead>
<tr><th>產(chǎn)品編號(hào)</th><th>產(chǎn)品名字</th><th>購(gòu)買數(shù)量</th><th>產(chǎn)品單價(jià)</th><th>產(chǎn)品總價(jià)</th><th>操作</th></tr>
</thead>
<tr ng-repeat="value in data">
<td>{{value.id}}</td>
<td>{{value.name}}</td>
<td>
<button class="btn btn-primary" ng-click="reduce(value.id)" >-</button>
<input type="number" name="num" ng-model="value.quantity" id="num">
<button class="btn btn-primary" ng-click="add(value.id)">+</button>
</td>
<td>{{value.price}}</td>
<td>{{value.price*value.quantity}}</td>
<td>
<button class="btn btn-danger" ng-click="removeItem(value.id)">移除</button>
</td>
</tr>
<tfoot>
<tr>
<td></td>
<td>總購(gòu)買數(shù)量 </td>
<td>{{totalQuantity()}}</td>
<td>總購(gòu)買價(jià)</td>
<td>{{totalPrice()}}</td>
<td>
<button class="btn btn-danger" ng-click="data=null">清空購(gòu)物車</button>
</td>
</tr>
</tfoot>
</table>
<p ng-show="!data.length">您的購(gòu)物車為空</p>
</div>
</body>
</html>
2、js邏輯部分
1 注冊(cè)應(yīng)用app
2 定義controller以即數(shù)據(jù)
3 在html中綁定應(yīng)用以及controller,實(shí)現(xiàn)數(shù)據(jù)渲染
var app=angular.module("app",[]);
var carController=function($scope){
$scope.data=[
{
id:1,
name:'HuaWei',
quantity:'2',
price:4300
},
{
id:2,
name:'iphone7',
quantity:'3',
price:6300
},
{
id:3,
name:'XiaoMi',
quantity:'3',
price:2800
},
{
id:4,
name:'Oppo',
quantity:'3',
price:2100
},
{
id:5,
name:'Vivo',
quantity:'3',
price:2100
}
]
}
注意:
1、在html中通過ng-app注冊(cè)應(yīng)用,ng-controller來綁定控制器作用域,ng-repeat遍歷商品數(shù)據(jù)data
2、在js中,angular提供了angular.forEach(obj,fn(item){})方法來遍歷angular的數(shù)據(jù),并可以在fn中對(duì)數(shù)據(jù)做進(jìn)一步處理。此處計(jì)算總價(jià)便通過此方法
二、業(yè)務(wù)邏輯
1、總價(jià)計(jì)算操作
/*
* 計(jì)算總價(jià)
* @method: angular.forEach()
* @param: 1. $scope.obj:要遍歷的scope上的數(shù)據(jù)
* 2. function(item){} 回調(diào),在函數(shù)內(nèi)部處理遍歷的數(shù)據(jù)
* */
$scope.totalPrice=function(){
var total=0;
angular.forEach($scope.data,function(item){
total+=item.quantity*item.price;
})
return total
}
/*計(jì)算總數(shù)量*/
$scope.totalQuantity=function(){
var total=0;
angular.forEach($scope.data,function(item){
total+=item.quantity;
})
return total
}
說明:
1)、使用angular提供的forEach方法遍歷當(dāng)前數(shù)據(jù),從而計(jì)算出所有數(shù)據(jù)的總價(jià)以及總數(shù)量
2、刪除條目
/*移除某項(xiàng)
* @param:傳入當(dāng)前項(xiàng)的id作為參數(shù),以確定移除哪一項(xiàng)
* */
$scope.removeItem=function(id){
var index=findIndex();
$scope.data.splice(index,1);
}
說明:
1)、為代碼利用,所以抽取出來findIndex()方法用于確定當(dāng)前操作的項(xiàng),代碼如下:
/*查找當(dāng)前操作的元素的位置*/
function findIndex(id){
var index=-1;
angular.forEach($scope.data,function(value,key,obj){
if(value.id===id){
index=key;
return index;
}
})
return index;
}
3.清空操作
在頁(yè)面中,直接利用表達(dá)式,但data=null完成數(shù)據(jù)的清空操作
<tr>
<td></td>
<td>總購(gòu)買數(shù)量 </td>
<td>{{totalQuantity()}}</td>
<td>總購(gòu)買價(jià)</td>
<td>{{totalPrice()}}</td>
<td>
<button class="btn btn-danger" ng-click="data=null">清空購(gòu)物車</button>
</td>
</tr>
4. 增加和刪除商品數(shù)量
/*增加商品數(shù)量*/
$scope.add=function(id){
var index=findIndex($scope,id),
item=$scope.data[index];
item.quantity++;
}
/*減少商品數(shù)量
* @description:
* 減少當(dāng)前項(xiàng)的數(shù)量,當(dāng)僅剩一件時(shí)彈出對(duì)話框確認(rèn)是否清空。是則調(diào)用removeItem方法清除當(dāng)前項(xiàng)
* */
$scope.reduce=function(id){
var index=findIndex($scope,id),
item=$scope.data[index];
if(item.quantity>1){ //判斷當(dāng)前項(xiàng)是否還能再減
item.quantity--;
}else{
var makesure=confirm('確定要清空當(dāng)前項(xiàng)嗎??');
if(makesure){
$scope.removeItem(id)
}
}
}
總結(jié):
此demo主要利用了angular的數(shù)據(jù)雙向綁定特性,從而大大的簡(jiǎn)化了操作。
關(guān)鍵點(diǎn):
1)、angular.forEach(obj,fn(value,key,obj)) 迭代器
2)、ng-click指令綁定點(diǎn)擊事件。使用ng-click會(huì)自動(dòng)觸發(fā)angular的臟檢查機(jī)制從而實(shí)時(shí)的更新視圖
3)、ng-repeat指令遍歷數(shù)據(jù),渲染頁(yè)面
4)、ng-show指令:通過其值來判斷是否顯示(原理是給元素加nghide類名)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Angularjs和bootstrap實(shí)現(xiàn)購(gòu)物車功能
- 使用Angular.js實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車功能
- Angularjs 制作購(gòu)物車功能實(shí)例代碼
- Angular實(shí)現(xiàn)購(gòu)物車計(jì)算示例代碼
- AngularJS 購(gòu)物車全選/取消全選功能的實(shí)現(xiàn)方法
- angular.js實(shí)現(xiàn)購(gòu)物車功能
- angularjs實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車功能
- AngularJS 實(shí)現(xiàn)購(gòu)物車全選反選功能
- AngularJs 終極購(gòu)物車(實(shí)例講解)
- Angular動(dòng)畫實(shí)現(xiàn)的2種方式以及添加購(gòu)物車動(dòng)畫實(shí)例代碼
相關(guān)文章
Angular 2.x學(xué)習(xí)教程之結(jié)構(gòu)指令詳解
結(jié)構(gòu)指令通過添加和刪除 DOM 元素來更改 DOM 布局。Angular 中兩個(gè)常見的結(jié)構(gòu)指令是 *ngIf 和 *ngFor,下面這篇文章主要給大家介紹了關(guān)于Angular 2.x結(jié)構(gòu)指令的相關(guān)資料,需要的朋友可以參考下。2017-05-05
angular2組件中定時(shí)刷新并清除定時(shí)器的實(shí)例講解
今天小編就為大家分享一篇angular2組件中定時(shí)刷新并清除定時(shí)器的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Angular.JS實(shí)現(xiàn)無(wú)限級(jí)的聯(lián)動(dòng)菜單(使用demo)
這篇文章主要介紹了Angular.JS中實(shí)現(xiàn)無(wú)限級(jí)聯(lián)動(dòng)菜單的使用示例,本文是在之前的一篇文章的基礎(chǔ)上進(jìn)行的幾個(gè)demo分享,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
angular2路由切換改變頁(yè)面title的示例代碼
本篇文章主要介紹了angular2路由切換改變頁(yè)面title的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
angular內(nèi)置provider之$compileProvider詳解
下面小編就為大家?guī)硪黄猘ngular內(nèi)置provider之$compileProvider詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示效果
本篇文章主要介紹了Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示的方法,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01

