本篇文章給大家分享的是有關使用Angular怎么計算購物車,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
專注于為中小企業(yè)提供網站設計、成都做網站服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)建華免費做網站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯網行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網站建設實現規(guī)模擴充和轉變。
實現過程:
一、使用任何語言創(chuàng)建一個服務端:
public class ShoppingCar { public string Title { get; set; } public decimal UnitPrice { get; set; } public int Count { get; set; } }
public ActionResult GetCar() { List<ShoppingCar> cars = new List<ShoppingCar> { new ShoppingCar { Title="蘋果",Count=1,UnitPrice=2.5m}, new ShoppingCar { Title="香蕉",Count=3,UnitPrice=1.5m}, new ShoppingCar { Title="苦瓜",Count=1,UnitPrice=3.5m}, new ShoppingCar { Title="黃瓜",Count=3,UnitPrice=2.2m} }; return Json(cars,JsonRequestBehavior.AllowGet); } public ActionResult AddCar(List<ShoppingCar> car) { return Json("ok", JsonRequestBehavior.AllowGet); }
二、前臺實現:
<div ng-app="DemoApp" ng-controller='CartController'> <table class="table table-striped"> <thead> <tr> <td>標題</td> <td>單價</td> <td>數量</td> <td>小計</td> <td>刪除</td> </tr> </thead> <tbody> <tr ng-repeat="item in ShoppingCar"> <td>{{item.Title}}</td> <td>{{item.UnitPrice}}</td> <td> <input type="text" ng-cloak ng-model="item.Count" > <button my-adds ng-click="UpdateCar(item.Title,1)" ng-class="{cursors:true}">+</button> <button my-minus ng-click="UpdateCar(item.Title,-1)" ng-class="{cursors:true}">-</button> </td> <td>{{item.Count*item.UnitPrice | number:2}}</td> <td><button my-minus ng-click="UpdateCar(item.Title,-100)" ng-class="{cursors:true}">刪</button></td> </tr> </tbody> </table> <p ng-init=0>總價格:{{ total | number:2}}</p> <button type="button" ng-click="submit()">提交</button> </div>
三、Angular部分
var app = angular.module('DemoApp', []); app.controller('CartController', ['$scope', '$http', function ($scope, $http) { $scope.ShoppingCar = {} var GetCar = function () { $http.get('/Employee/GetCar') .success(function (response) { $scope.ShoppingCar = response; GetTotal(); }); } $scope.total = 0; var GetTotal = function () { for (var i = 0; i < $scope.ShoppingCar.length; i++) { var item = $scope.ShoppingCar[i]; $scope.total += item.Count * item.UnitPrice; } } $scope.UpdateCar = function (title, count) { for (var i = 0; i < $scope.ShoppingCar.length; i++) { var item = $scope.ShoppingCar[i]; if (item.Title == title) { item.Count = item.Count + count;//這里可以增加上下限制 if (item.Count < 0) { $scope.ShoppingCar.splice(i, 1); } } } GetTotal(); } $scope.submit = function () { $http.post('/Employee/AddCar', $scope.ShoppingCar) .success(function (response) { alert(response); }); } GetCar(); }]);
以上就是使用Angular怎么計算購物車,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯行業(yè)資訊頻道。
網站題目:使用Angular怎么計算購物車
網頁地址:http://chinadenli.net/article48/gehpep.html
成都網站建設公司_創(chuàng)新互聯,為您提供小程序開發(fā)、響應式網站、全網營銷推廣、虛擬主機、網站設計、用戶體驗
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯