Angular實現(xiàn)購物車計算示例代碼
使用AngularJS實現(xiàn)一個簡單的購物車,主要感受強大的雙向綁定和只關(guān)注對象不關(guān)注界面特性。
先看看界面:
點擊+-操作和刪除:
這些全部只需要操作數(shù)據(jù)源就行,不需要關(guān)注界面。
實現(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); }
二、前臺實現(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>小計</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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法
本篇文章主要介紹了詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法,非常具有實用價值,需要的朋友可以參考下2017-05-05Angularjs中ng-repeat-start與ng-repeat-end的用法實例介紹
這篇文章主要給大家介紹了Angularjs中ng-repeat-start與ng-repeat-end的用法,文章開始先進行了簡單的介紹,而后通過完整的實例代碼詳細(xì)給大家介紹這兩者的用法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12Angular6使用forRoot() 注冊單一實例服務(wù)問題
這篇文章主要介紹了Angular6使用forRoot() 注冊單一實例服務(wù)問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08angularjs+bootstrap實現(xiàn)自定義分頁的實例代碼
本篇文章主要介紹了angularjs+bootstrap實現(xiàn)自定義分頁的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06