当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > Angular实现购物车计算示例代码

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

2018年05月01日  | 移动技术网IT编程  | 我要评论

使用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();
  }]);

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网