欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

angular和BootStrap3實現(xiàn)購物車功能

 更新時間:2017年01月25日 08:35:19   作者:Sophie_U  
這篇文章主要為大家詳細介紹了angular和BootStrap3實現(xiàn)購物車功能的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、頁面搭建

1、html結(jié)構(gòu)

采用BootStrap3來快速創(chuàng)建一個購物車表單樣式。
主要功能:
A:列表數(shù)據(jù)所示
B:增加刪除端口
C:清空購物車
D:商品數(shù)量的增減
E:動態(tài)計算商品價格以及總價

<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
 <meta charset="UTF-8">
 <title>購物車</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)品編號</th><th>產(chǎn)品名字</th><th>購買數(shù)量</th><th>產(chǎn)品單價</th><th>產(chǎn)品總價</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>總購買數(shù)量 </td>
    <td>{{totalQuantity()}}</td>
    <td>總購買價</td>
    <td>{{totalPrice()}}</td>
    <td>
     <button class="btn btn-danger" ng-click="data=null">清空購物車</button>
    </td>
   </tr>
   </tfoot>
  </table>
  <p ng-show="!data.length">您的購物車為空</p>
 </div>
</body>
</html>

2、js邏輯部分

1 注冊應(yīng)用app
2 定義controller以即數(shù)據(jù)
3 在html中綁定應(yīng)用以及controller,實現(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注冊應(yīng)用,ng-controller來綁定控制器作用域,ng-repeat遍歷商品數(shù)據(jù)data
2、在js中,angular提供了angular.forEach(obj,fn(item){})方法來遍歷angular的數(shù)據(jù),并可以在fn中對數(shù)據(jù)做進一步處理。此處計算總價便通過此方法

二、業(yè)務(wù)邏輯

1、總價計算操作

/*
   * 計算總價
   * @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
  }
   /*計算總數(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ù),從而計算出所有數(shù)據(jù)的總價以及總數(shù)量

2、刪除條目

 /*移除某項
  * @param:傳入當(dāng)前項的id作為參數(shù),以確定移除哪一項
  * */
  $scope.removeItem=function(id){
   var index=findIndex();
   $scope.data.splice(index,1);
  }

說明:

1)、為代碼利用,所以抽取出來findIndex()方法用于確定當(dā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.清空操作

在頁面中,直接利用表達式,但data=null完成數(shù)據(jù)的清空操作

<tr>
 <td></td>
 <td>總購買數(shù)量 </td>
 <td>{{totalQuantity()}}</td>
 <td>總購買價</td>
 <td>{{totalPrice()}}</td>
 <td>
  <button class="btn btn-danger" ng-click="data=null">清空購物車</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)前項的數(shù)量,當(dāng)僅剩一件時彈出對話框確認是否清空。是則調(diào)用removeItem方法清除當(dāng)前項
  * */
  $scope.reduce=function(id){
   var index=findIndex($scope,id),
    item=$scope.data[index];
   if(item.quantity>1){ //判斷當(dāng)前項是否還能再減
    item.quantity--;
   }else{
    var makesure=confirm('確定要清空當(dāng)前項嗎??');
    if(makesure){
     $scope.removeItem(id)
    }
   }
  }

總結(jié):
此demo主要利用了angular的數(shù)據(jù)雙向綁定特性,從而大大的簡化了操作。
關(guān)鍵點:

1)、angular.forEach(obj,fn(value,key,obj)) 迭代器
2)、ng-click指令綁定點擊事件。使用ng-click會自動觸發(fā)angular的臟檢查機制從而實時的更新視圖
3)、ng-repeat指令遍歷數(shù)據(jù),渲染頁面
4)、ng-show指令:通過其值來判斷是否顯示(原理是給元素加nghide類名)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • monaco?editor在Angular的使用詳解

    monaco?editor在Angular的使用詳解

    這篇文章主要為大家介紹了monaco?editor在Angular的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Angular 2.x學(xué)習(xí)教程之結(jié)構(gòu)指令詳解

    Angular 2.x學(xué)習(xí)教程之結(jié)構(gòu)指令詳解

    結(jié)構(gòu)指令通過添加和刪除 DOM 元素來更改 DOM 布局。Angular 中兩個常見的結(jié)構(gòu)指令是 *ngIf 和 *ngFor,下面這篇文章主要給大家介紹了關(guān)于Angular 2.x結(jié)構(gòu)指令的相關(guān)資料,需要的朋友可以參考下。
    2017-05-05
  • AngularJS中的過濾器filter用法完全解析

    AngularJS中的過濾器filter用法完全解析

    這篇文章主要介紹了AngularJS中的過濾器filter用法,包括Angular中一些常用的自帶的過濾器的列舉以及自定義filter的方法,需要的朋友可以參考下
    2016-04-04
  • 深入理解Angular.JS中的Scope繼承

    深入理解Angular.JS中的Scope繼承

    這篇文章主要給大家深入的介紹了關(guān)于Angular.JS中Scope繼承的相關(guān)資料,Angular。JS中scope之間的繼承關(guān)系使用JavaScript的原型繼承方式實現(xiàn),文中介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-06-06
  • angular2組件中定時刷新并清除定時器的實例講解

    angular2組件中定時刷新并清除定時器的實例講解

    今天小編就為大家分享一篇angular2組件中定時刷新并清除定時器的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Angular.JS實現(xiàn)無限級的聯(lián)動菜單(使用demo)

    Angular.JS實現(xiàn)無限級的聯(lián)動菜單(使用demo)

    這篇文章主要介紹了Angular.JS中實現(xiàn)無限級聯(lián)動菜單的使用示例,本文是在之前的一篇文章的基礎(chǔ)上進行的幾個demo分享,有需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • angular2路由切換改變頁面title的示例代碼

    angular2路由切換改變頁面title的示例代碼

    本篇文章主要介紹了angular2路由切換改變頁面title的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 關(guān)于 angularJS的一些用法

    關(guān)于 angularJS的一些用法

    這篇文章主要介紹了關(guān)于 angularJS的一些用法的相關(guān)資料,需要的朋友可以參考下
    2017-11-11
  • angular內(nèi)置provider之$compileProvider詳解

    angular內(nèi)置provider之$compileProvider詳解

    下面小編就為大家?guī)硪黄猘ngular內(nèi)置provider之$compileProvider詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Angularjs實現(xiàn)搜索關(guān)鍵字高亮顯示效果

    Angularjs實現(xiàn)搜索關(guān)鍵字高亮顯示效果

    本篇文章主要介紹了Angularjs實現(xiàn)搜索關(guān)鍵字高亮顯示的方法,具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01

最新評論