<! DOCTYPE Html><script type= "Text/javascript"; var app = Angular.module ("Mapp", []); App.controller ("Mctrl", function ($scope) {//Data source $scope. Items = [{"Name": "Wang Lao Ji", "Price": 4}, {"name": "Old Godmother", "Price": 7.5}, {"name": "Stinky Tofu", "Price": 7 . +}, {"name": "Cookie", "Price": 22},]; var len = $scope. items.length; var arr = []; Loop array assignment for (var i = 0; i < len; i++) {arr[i] = 0; }//Detect changes, for the total price can change $scope. $watch ("Varlist.itemnum", function () {gettotal (); }, True); Defines the variable $scope. varlist = {Itemnum:arr, total:0}//When shopping $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 (); }//Calculate Total price 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] * $s Cope.items[j].price; } return $scope. varlist.total; }});</script></body>
2015-01-19 10:54:27 Linjs
ANGULARJS Realization Shopping Cart