AngularJS 카트 구현

AngularJS를 사용하여 다음과 같은 카트를 구성합니다.
html>



    
    
    



    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
`item`.`id``item`.`name`                                                                                                `item`.`price`{{item.quantity*item.price}}                                              
:{{totalPrice()}}  :{{totalQuantity()}}                                              
    
              angular.module('app', []).controller('firstController',         function($scope) {             $scope.Product = [{                 id: 1000,                 name: "iPhone 6 Plus",                 quantity: 1,                 price: 6888             }, {                 id: 1001,                 name: "iPhone 6",                 quantity: 1,                 price: 5288             }, {                 id: 1002,                 name: "iPhone 5s",                 quantity: 1,                 price: 4188             }, {                 id: 1003,                 name: "iPhone 5c",                 quantity: 1,                 price: 3288             }];             $scope.totalPrice = function() {                 var total = 0;                 angular.forEach($scope.Product, function(item) {                     total += item.quantity * item.price;                 });                 return total;             }             $scope.totalQuantity = function() {                 var total = 0;                 angular.forEach($scope.Product, function(item) {                     total += parseInt(item.quantity);                 });                 return total;             }             $scope.remove = function(index) {                 $scope.Product.splice(index, 1);             }             $scope.removeall = function() {                 var index;                 for (index = $scope.Product.length - 1; index >= 0; index--) {                     $scope.remove(index);                 }             }             $scope.reduce = function(index) {                 if ($scope.Product[index].quantity != 1) {                     $scope.Product[index].quantity--;                 } else {                     var ans = confirm(" ?");                     if (ans) {                         $scope.remove(index);                     } else {                         $scope.Product[index].quantity = 1;                     }                 }             }             $scope.add = function(index) {                 $scope.Product[index].quantity++;             }             $scope.$watch('Product', function(newValue, oldValue) {                 angular.forEach(newValue, function(item, key) {                     if (item.quantity < 1) {                         var ans = confirm(" ?");                         if (ans) {                             $scope.remove(key);                         } else {                             item.quantity = oldValue[key].quantity;                         }                         return;                     }                 });             }, true);         });     

n-repeat는 순환을 실현하고,ng-click 뒤에서 클릭한 후 터치하는 이벤트는first Controller 내의 몇 가지 함수는 자신이 쓴 것입니다.
이곳에서는 카트 상품의 증감을 실현하고 총가와 건수가 이에 따라 변화하며 상품은 제거할 수 있고 카트는 비울 수 있다.

좋은 웹페이지 즐겨찾기