Angular實現(xiàn)購物車計算示例代碼
使用AngularJS實現(xiàn)一個簡單的購物車,主要感受強大的雙向綁定和只關(guān)注對象不關(guān)注界面特性。
先看看界面:

點擊+-操作和刪除:

這些全部只需要操作數(shù)據(jù)源就行,不需要關(guān)注界面。
實現(xiàn)過程:
一、使用任何語言創(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);
}
二、前臺實現(xiàn):
<div ng-app="DemoApp" ng-controller='CartController'>
<table class="table table-striped">
<thead>
<tr>
<td>標題</td>
<td>單價</td>
<td>數(shù)量</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" style="width:50px;text-align:center;">
<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();
}]);
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS基礎(chǔ) ng-keypress 指令簡單示例
本文主要介紹AngularJS ng-keypress 指令,這里對ng-keypress指令的基礎(chǔ)資料整理,并附有實例代碼,需要的小伙伴參考下2016-08-08
Angular通過?HTTP?Interceptor?實現(xiàn)?HTTP?請求超時監(jiān)控的例子
這篇文章主要介紹了Angular?如何通過?HTTP?Interceptor?實現(xiàn)?HTTP?請求的超時監(jiān)控,本文通過例子給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
AngularJS使用ng-repeat和ng-if實現(xiàn)數(shù)據(jù)的刪選顯示效果示例【適用于表單數(shù)據(jù)的顯示】
這篇文章主要介紹了AngularJS使用ng-repeat和ng-if實現(xiàn)數(shù)據(jù)的刪選顯示效果,非常適用于表單數(shù)據(jù)的顯示使用,涉及ng-repeat和ng-if命令的相關(guān)使用技巧,需要的朋友可以參考下2016-12-12
Angular中的結(jié)構(gòu)指令模式及使用詳解
這篇文章主要為大家介紹了Angular中的結(jié)構(gòu)指令模式及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
Angular.js?實現(xiàn)帶手柄自由調(diào)整頁面大小的功能
這篇文章主要介紹了Angular.js?實現(xiàn)帶手柄自由調(diào)整頁面大小的功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12

