angular實現購物車自動計算價格程式碼例項
分享一段程式碼例項,它利用angular實現了購物車自動計算價格的功能。
點選或者減少商品的數量會自動實現價格總額的計算。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html lang="en" ng-app="myCart"> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"> <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body ng-controller="cartCtr"> <div class="container"> <table class="table"> <thead> <tr> <th>產品編號</th> <th>產品名字</th> <th>購買數量</th> <th>產品單價</th> <th>產品總價</th> <th>操作</th> </tr> </thead> <tbody> <tr ng-repeat="item in cart"> <td>{{item.id}}</td> <td>{{item.name}}</td> <td> <button type="button" ng-click="reduce($index)" class="btn btn-primary">-</button> <input type="number" id="inp" value="{{item.quantity}}" ng-model="item.quantity"/> <button type="button" ng-click="add($index)" class="btn btn-primary">+</button> </td> <td>{{item.price}}</td> <td>{{item.quantity*item.price}}</td> <td><button type="button" class="btn btn-danger" ng-click="remove($index)">刪除</button></td> </tr> <tr ng-show="!cart.length"> <td colspan="6">您的購物車為空</td> </tr> <tr> <td>總購買價格:{{totalPrice()}}</td> <td>總購買數量:{{totalQuantity()}}</td> <td colspan="4" align="center"><button type="button" ng-click="cart={}" class="btn btn-danger">清空購物車</button></td> </tr> </tbody> </table> </div> <script> var mod = angular.module("myCart", []); mod.service("cartData", function() { return [{ id: 1000, name: 'iphone5s', quantity: 3, price: 4300 }, { id: 3300, name: 'iphone5', quantity: 30, price: 3300 }, { id: 232, name: 'imac', quantity: 4, price: 23000 }, { id: 1400, name: 'ipad', quantity: 5, price: 6900 }]; }); mod.controller("cartCtr", ["$scope", "cartData", function($scope, cd) { //把注入進來的資料cd,儲存到變數cart中 $scope.cart = cd; //當點選刪除按鈕的時候,刪除當前購物車的一條資料 $scope.remove = function(index) { $scope.cart.splice(index, 1); } //得到總購買價格 $scope.totalPrice = function() { var total = 0; angular.forEach($scope.cart, function(item) { total += item.price * item.quantity; }) return total; } //得到總購買數量 $scope.totalQuantity = function() { var total = 0; angular.forEach($scope.cart, function(item) { total += parseInt(item.quantity); }) return total; } //點選+號的時候,數量加一 $scope.add = function(index) { $scope.cart[index].quantity++; } //點選-號的時候,數量減一,當減到<1的時候,直接刪除這一條資料 $scope.reduce = function(index) { if ($scope.cart[index].quantity > 1) { $scope.cart[index].quantity--; } else { $scope.remove(index); } } //當購物車某商品的數量小於1時,輸入的值指定為oldValue $scope.$watch("cart", function(newValue, oldValue) { console.log(newValue, oldValue) angular.forEach(newValue, function(item, key) { if (item.quantity < 1) { item.quantity = oldValue[key].quantity; } }) }, true) }]) </script> </body> </html>
相關文章
- JavaScript 購物車自動計算價格JavaScript
- 購物車自動計算商品總價格
- JavaScript商城購物車價格自動計算功能JavaScript
- angularjs實現自動計算商品總價格程式碼例項AngularJS
- JavaScript購物車計算商品總價格JavaScript
- angularjs實現的購物車效果程式碼例項AngularJS
- 通過拖動將商品新增到購物車效果程式碼例項
- vue例項-購物車功能Vue
- 輸入單價和數量自動計算價格程式碼
- 原生js實現購物車結算JS
- day85:luffy:購物車根據有效期不同切換價格&購物車刪除操作&價格結算&訂單頁面前戲
- nodeJS程式碼實現計算交社保程式碼例項NodeJS
- asp.net 實現購物車詳細程式碼ASP.NET
- jquery外掛實現圖片可拖動的購物車程式碼jQuery
- 購物車的實現及結算處理
- jQuery實現的自動播放簡單程式碼例項jQuery
- css3實現的動物頭像效果程式碼例項CSSS3
- 購物車的實現原理
- 購物車原理以及實現
- Vue實現購物車效果Vue
- React實現購物車功能React
- ASP.NET購物車的實現及結算處理原始碼ASP.NET原始碼
- js實現的冪運算程式碼例項JS
- 根據商品數量自動計算價格
- 直播網站程式原始碼,採用Redis實現購物車功能網站原始碼Redis
- 加入購物車動畫效果實現動畫
- 【jquery】實現購物車加減jQuery
- javascript 計算器程式碼例項JavaScript
- jQuery實現滑動門程式碼例項jQuery
- 基於ReactiveCocoa和MVVM設計的購物車基本操作實現程式碼解析ReactMVVM
- Android實現商城購物車功能Android
- vue2.0實現購物車功能Vue
- 二級列表完美實現購物車
- AngularJS 實現簡單購物車AngularJS
- jQuery實現的監聽回車按鍵程式碼例項jQuery
- 點選回車實現表單提交效果程式碼例項
- 直播app原始碼,map實現購物車選擇功能APP原始碼
- js實現的元素運動程式碼例項JS