国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Angular實現購物車計算示例代碼

2019-11-19 17:28:12
字體:
來源:轉載
供稿:網友

使用AngularJS實現一個簡單的購物車,主要感受強大的雙向綁定和只關注對象不關注界面特性。

先看看界面:

點擊+-操作和刪除:

這些全部只需要操作數據源就行,不需要關注界面。

實現過程:

一、使用任何語言創(chuàng)建一個服務端:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 闽侯县| 金塔县| 高州市| 天等县| 贵港市| 苍山县| 同仁县| 临潭县| 高雄县| 尖扎县| 米脂县| 遂宁市| 巴林左旗| 深圳市| 旬阳县| 平谷区| 独山县| 上杭县| 双流县| 天等县| 安宁市| 叙永县| 琼结县| 眉山市| 湟中县| 平潭县| 陕西省| 茂名市| 巴塘县| 大新县| 宜昌市| 惠安县| 澜沧| 怀柔区| 自治县| 富阳市| 浦城县| 昌乐县| 南投市| 威信县| 任丘市|