您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)使用Angular怎么計(jì)算購物車,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
實(shí)現(xiàn)過程:
一、使用任何語言創(chuàng)建一個服務(wù)端:
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); }
二、前臺實(shí)現(xiàn):
<div ng-app="DemoApp" ng-controller='CartController'> <table class="table table-striped"> <thead> <tr> <td>標(biāo)題</td> <td>單價</td> <td>數(shù)量</td> <td>小計(jì)</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怎么計(jì)算購物車,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。