• Angular实现购物车计算


     

    使用AngularJS实现一个简单的购物车,主要感受强大的双向绑定和只关注对象不关注界面特性。

    先看看界面:

    image

    点击+-操作和删除:

    image

    这些全部只需要操作数据源就行,不需要关注界面。

    实现过程:

    一、使用任何语言创建一个服务端:

        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);
            }
    复制代码

    二、前台实现:

    复制代码
        <div ng-app="DemoApp" ng-controller='CartController'>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <td>标题</td>
                        <td>单价</td>
                        <td>数量</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="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();
        }]);
  • 相关阅读:
    用Tinkercad学arduino之 LCD电压电流表
    用Tinkercad学arduino之 L293D电机驱动器驱动直流电机
    用Tinkercad学arduino之 74HC595 寄存器控制1位7段数码管
    用Tinkercad学arduino之 74HC595寄存器控制8个led跑马灯
    用Tinkercad学arduino之 红外遥控器
    用Tinkercad学arduino之 人体红外检测报警 LCD显示
    用Tinkercad学arduino之 人体红外检测报警 蜂鸣器+LED
    用Tinkercad学arduino之 电位器控制伺服电机转向
    用Tinkercad学arduino之 光线控制彩灯
    用Tinkercad学arduino之 温度LED报警
  • 原文地址:https://www.cnblogs.com/wj19940520/p/6409806.html
Copyright © 2020-2023  润新知