Angular實(shí)現(xiàn)購(gòu)物車計(jì)算示例代碼
使用AngularJS實(shí)現(xiàn)一個(gè)簡(jiǎn)單的購(gòu)物車,主要感受強(qiáng)大的雙向綁定和只關(guān)注對(duì)象不關(guān)注界面特性。
先看看界面:

點(diǎn)擊+-操作和刪除:

這些全部只需要操作數(shù)據(jù)源就行,不需要關(guān)注界面。
實(shí)現(xiàn)過程:
一、使用任何語(yǔ)言創(chuàng)建一個(gè)服務(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);
}
二、前臺(tái)實(shí)現(xiàn):
<div ng-app="DemoApp" ng-controller='CartController'>
<table class="table table-striped">
<thead>
<tr>
<td>標(biāo)題</td>
<td>單價(jià)</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" 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>總價(jià)格:{{ 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();
}]);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Angularjs和bootstrap實(shí)現(xiàn)購(gòu)物車功能
- 使用Angular.js實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車功能
- Angularjs 制作購(gòu)物車功能實(shí)例代碼
- angular和BootStrap3實(shí)現(xiàn)購(gòu)物車功能
- AngularJS 購(gòu)物車全選/取消全選功能的實(shí)現(xiàn)方法
- angular.js實(shí)現(xiàn)購(gòu)物車功能
- angularjs實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車功能
- AngularJS 實(shí)現(xiàn)購(gòu)物車全選反選功能
- AngularJs 終極購(gòu)物車(實(shí)例講解)
- Angular動(dòng)畫實(shí)現(xiàn)的2種方式以及添加購(gòu)物車動(dòng)畫實(shí)例代碼
相關(guān)文章
AngularJS基礎(chǔ) ng-keypress 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-keypress 指令,這里對(duì)ng-keypress指令的基礎(chǔ)資料整理,并附有實(shí)例代碼,需要的小伙伴參考下2016-08-08
AngularJs實(shí)現(xiàn)分頁(yè)功能不帶省略號(hào)的代碼
這篇文章主要介紹了AngularJs實(shí)現(xiàn)分頁(yè)功能不帶省略號(hào)的代碼的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-05-05
Angular通過?HTTP?Interceptor?實(shí)現(xiàn)?HTTP?請(qǐng)求超時(shí)監(jiān)控的例子
這篇文章主要介紹了Angular?如何通過?HTTP?Interceptor?實(shí)現(xiàn)?HTTP?請(qǐng)求的超時(shí)監(jiān)控,本文通過例子給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
AngularJS使用ng-repeat和ng-if實(shí)現(xiàn)數(shù)據(jù)的刪選顯示效果示例【適用于表單數(shù)據(jù)的顯示】
這篇文章主要介紹了AngularJS使用ng-repeat和ng-if實(shí)現(xiàn)數(shù)據(jù)的刪選顯示效果,非常適用于表單數(shù)據(jù)的顯示使用,涉及ng-repeat和ng-if命令的相關(guān)使用技巧,需要的朋友可以參考下2016-12-12
Angular中的結(jié)構(gòu)指令模式及使用詳解
這篇文章主要為大家介紹了Angular中的結(jié)構(gòu)指令模式及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Angular.js?實(shí)現(xiàn)帶手柄自由調(diào)整頁(yè)面大小的功能
這篇文章主要介紹了Angular.js?實(shí)現(xiàn)帶手柄自由調(diào)整頁(yè)面大小的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
Angular2利用組件與指令實(shí)現(xiàn)圖片輪播組件
這篇文章主要給大家介紹了Angular2中組件與指令的一個(gè)小實(shí)踐,利用組件和指令實(shí)現(xiàn)一個(gè)圖片輪播組件的相關(guān)資料,文中給出了詳細(xì)的介紹和示例代碼,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-03-03

