AngularJS實現 購物車

來源:互聯網
上載者:User

標籤:

<!DOCTYPE html><html><head><meta charset = "utf-8"><script type="text/javascript" src = "lib/angular.js"></script><style type="text/css">   .item{    height: 200px;    width:100px;    margin:5px;    border:1px solid #ccc;    float:left;   }</style></head><body ng-app = "mapp" ng-controller = "mctrl"><div class = "list"><div class = "item" ng-repeat = "item in items"><p class = "item-name"><span>商品名:</span>{{item.name}}</p><p class = "item-price"><span>價格:</span>{{item.price|currency:"¥"}}</p><input type = "button" ng-click = "buy($index)" value = "購買"></div></div><div class = "item-car"><p ng-repeat = "item in items"><span>{{item.name}}&nbsp&nbsp&nbsp&nbsp<span><span>{{item.price|currency:"單價"}}&nbsp&nbsp&nbsp</span><input type = "button" value = "-" ng-click = "minus($index)"/><input type = "text" ng-model = "varlist.itemNum[$index]"><input type = "button" value = "+" ng-click = "add($index)"/><span>單件總價:{{item.price*varlist.itemNum[$index]|currency:"¥"}}</span></p><p>總價:{{varlist.total|number:2}}</p></div><script type="text/javascript">var app = angular.module("mapp", []);app.controller("mctrl", function($scope) {// 資料來源    $scope.items = [{        "name": "王老吉",        "price": 4    }, {        "name": "老乾媽",        "price": 7.5    }, {        "name": "臭豆腐",        "price": 7.32    }, {        "name": "餅乾",        "price": 22    }, ];    var len = $scope.items.length;    var arr = [];    // 迴圈為數組賦值    for (var i = 0; i < len; i++) {        arr[i] = 0;    }    // 檢測是否變化,為了總價能夠改變    $scope.$watch("varlist.itemNum", function() {        getTotal();    }, true);    // 定義變數    $scope.varlist = {        itemNum: arr,        total: 0    }    // 購物的時候操作    $scope.buy = function($index) {        $scope.varlist.itemNum[$index]++;        getTotal();    }    // -----    $scope.minus = function($index) {        if ($scope.varlist.itemNum[$index] == 0) {            return;        } else {            $scope.varlist.itemNum[$index]--;            getTotal();        }    }    // ++++    $scope.add = function($index) {        $scope.varlist.itemNum[$index]++;        getTotal();    }    // 計算總價    var getTotal = function($index) {        $scope.varlist.total = 0;        for (var j = 0; j < len; j++) {            $scope.varlist.total = $scope.varlist.total + $scope.varlist.itemNum[j] * $scope.items[j].price;        }        return $scope.varlist.total;    }});</script></body></html>

2015-01-19 10:54:27 linJS

  

AngularJS實現 購物車

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.