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

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

這些全部只需要操作數(shù)據(jù)源就行,不需要關(guān)注界面。
實(shí)現(xiàn)過(guò)程:
一、使用任何語(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="蘋(píng)果",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)物車(chē)功能
- 使用Angular.js實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)功能
- Angularjs 制作購(gòu)物車(chē)功能實(shí)例代碼
- angular和BootStrap3實(shí)現(xiàn)購(gòu)物車(chē)功能
- AngularJS 購(gòu)物車(chē)全選/取消全選功能的實(shí)現(xiàn)方法
- angular.js實(shí)現(xiàn)購(gòu)物車(chē)功能
- angularjs實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)功能
- AngularJS 實(shí)現(xiàn)購(gòu)物車(chē)全選反選功能
- AngularJs 終極購(gòu)物車(chē)(實(shí)例講解)
- Angular動(dòng)畫(huà)實(shí)現(xiàn)的2種方式以及添加購(gòu)物車(chē)動(dòng)畫(huà)實(shí)例代碼
相關(guān)文章
AngularJS 表單驗(yàn)證手機(jī)號(hào)的實(shí)例(非必填)
下面小編就為大家?guī)?lái)一篇AngularJS 表單驗(yàn)證手機(jī)號(hào)的實(shí)例(非必填)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
AngularJS 仿微信圖片手勢(shì)縮放的實(shí)例
這篇文章主要介紹了AngularJS 仿微信圖片手勢(shì)縮放的實(shí)例的相關(guān)資料,希望大家通過(guò)本文實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法
本篇文章主要介紹了詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
AngularJS入門(mén)教程之AngularJS指令
AngularJS指令用于擴(kuò)展HTML。本文給大家介紹AngularJS入門(mén)教程之AngularJS指令,感興趣的朋友一起學(xué)習(xí)吧2016-04-04
Angular實(shí)現(xiàn)雙向折疊列表組件的示例代碼
本篇文章主要介紹了Angular實(shí)現(xiàn)雙向折疊列表組件的示例代碼,分為左右兩組,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Angularjs中ng-repeat-start與ng-repeat-end的用法實(shí)例介紹
這篇文章主要給大家介紹了Angularjs中ng-repeat-start與ng-repeat-end的用法,文章開(kāi)始先進(jìn)行了簡(jiǎn)單的介紹,而后通過(guò)完整的實(shí)例代碼詳細(xì)給大家介紹這兩者的用法,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12
Angular6使用forRoot() 注冊(cè)單一實(shí)例服務(wù)問(wèn)題
這篇文章主要介紹了Angular6使用forRoot() 注冊(cè)單一實(shí)例服務(wù)問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
angularjs+bootstrap實(shí)現(xiàn)自定義分頁(yè)的實(shí)例代碼
本篇文章主要介紹了angularjs+bootstrap實(shí)現(xiàn)自定義分頁(yè)的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
angular實(shí)現(xiàn)導(dǎo)航菜單切換
這篇文章主要為大家詳細(xì)介紹了angular實(shí)現(xiàn)導(dǎo)航菜單切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

