Angular实现购物车计算示例代码

 更新时间:2017年02月21日 16:59:38   作者:奕佚  
本篇文章主要介绍了Angular实现购物车计算示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

先看看界面:

点击+-操作和删除:

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

实现过程:

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

  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="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();
  }]);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • AngularJs  Understanding Angular Templates

    AngularJs Understanding Angular Templates

    本文主要介绍AngularJs Understanding Angular Templates的资料,这里整理了详细的资料及简单示例代码,有兴趣的小伙伴的参考下
    2016-09-09
  • Angularjs使用过滤器完成排序功能

    Angularjs使用过滤器完成排序功能

    这篇文章主要为大家详细介绍了Angularjs使用过滤器完成排序功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • AngularJS实现ajax请求的方法

    AngularJS实现ajax请求的方法

    这篇文章主要介绍了AngularJS实现ajax请求的方法,结合实例形式分析了AngularJS实现ajax请求的前端界面、ajax交互及后台php处理技巧,需要的朋友可以参考下
    2016-11-11
  • AngularJS入门教程之Hello World!

    AngularJS入门教程之Hello World!

    这篇文章主要介绍了AngularJS入门教程之Hello World!,本文用经典的应用程序“Hello World!”来讲解AngularJS,要的朋友可以参考下
    2014-12-12
  • Angularjs实现搜索关键字高亮显示效果

    Angularjs实现搜索关键字高亮显示效果

    本篇文章主要介绍了Angularjs实现搜索关键字高亮显示的方法,具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • Angularjs自定义指令Directive详解

    Angularjs自定义指令Directive详解

    Directive是一个非常棒的功能。可以实现我们自义的的功能方法。下面通过实例代码给大家介绍Angularjs自定义指令Directive相关知识,感兴趣的朋友一起学习吧
    2017-05-05
  • 基于datepicker定义自己的angular时间组件的示例

    基于datepicker定义自己的angular时间组件的示例

    这篇文章主要介绍了基于datepicker定义自己的angular时间组件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • angular6的响应式表单的实现

    angular6的响应式表单的实现

    这篇文章主要介绍了angular6的响应式表单的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • monaco editor在Angular的使用详解

    monaco editor在Angular的使用详解

    这篇文章主要为大家介绍了monaco editor在Angular的使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • AngularJs 指令详解及示例代码

    AngularJs 指令详解及示例代码

    本文主要介绍AngularJs 指令的知识,这里整理了详细的资料和简单示例代码帮助大家学习理解应用,有兴趣的小伙伴可以参考下
    2016-09-09

最新评论