2017-03-05 20 views
1

這裏是我的簡單的購物車,我有產品列表,當我點擊每個項目時,我得到的細節,我已添加到購物車按鈕的細節頁面,該產品添加到購物車,我也執行刪除購物車的邏輯。我想向用戶提供一個選擇角度的購物車產品數量的選項嗎?

plunker with example

但我想向用戶提供與選擇的產品的數量的選項,用戶可以增加或減少的數量是多少?另外,購物車中的物品數量應該有限制!任何人都可以提出我可以如何實現這一點?

這將是,如果有人提出任何被添加到代碼,這將是方便用戶和良好的結構化的方式提高效率真的有用嗎?

var app = angular.module("myApp", ["ngRoute"]); 
app.controller('mobileController', function($scope) { 
    $scope.items = [{ 
    name: 'Iphone', 
    price: 70000, 
    rating: '*****', 
    image: 'http://i.imgur.com/hfMaGTN.png' 
    }, { 
    name: 'Oneplus', 
    price: 60000, 
    rating: '****', 
    image: 'http://i.imgur.com/sBcs5ZE.jpg' 
    }, { 
    name: 'Samsung', 
    price: 50000, 
    rating: '***', 
    image: 'http://i.imgur.com/8Bexxbf.jpg' 
    }, { 
    name: 'Sony', 
    price: 40000, 
    rating: '***', 
    image: 'http://i.imgur.com/0c7PcX8.png' 
    }, { 
    name: 'Moto', 
    price: 20000, 
    rating: '****', 
    image: 'http://i.imgur.com/HyWR1VE.png' 
    }]; 
}); 

app.service("cartService", [function(){ 

    var cart = []; 

    function getCart(){ 
    console.log(cart); 
    return cart; 
    } 

    function addToCart(item){ 
    cart.push(item); 
    console.log(cart); 
    } 

    return { 
    getCart: getCart, 
    addToCart: addToCart 
    }; 

}]); 

app.config(function($routeProvider) { 
    $routeProvider 

    .when("/store", { 
     templateUrl : "store.html", 
    }) 

.when('/item/:itemName', { 
     templateUrl: 'details.html', 
     controller: 'ItemCtrl' 
    }) 
    .when("/cart", { 
     templateUrl: 'cartdetails.html', 
     controller: 'cartCtrl' 
    }) 
    .when("/checkout", { 
     templateUrl: 'checkout.html', 
     controller: 'cartCtrl' 
    }); 


}); 

app.controller('ItemCtrl', ['$scope', '$routeParams', 'cartService', 
    function($scope, $routeParams, cartService) { 
    $scope.item = {}; 
    angular.forEach($scope.items, function(item) { 
     if (item.name == $routeParams.itemName) { 
     $scope.item.itemName = item.name; 
     $scope.item.itemPrice = item.price; 
     $scope.item.itemRating = item.rating; 
     $scope.item.itemImage = item.image; 
     } 
    }); 

    $scope.addProduct = function(item){ 
     console.log(item); 
     cartService.addToCart(item); 
     alert(item.itemName+" added successfully") ; 
    }; 

    } 
]); 
app.controller('cartCtrl', ['$scope','cartService', 
    function($scope,cartService) { 
    $scope.name="saisree"; 

    $scope.cartItems=cartService.getCart(); 

    $scope.getTotal = function(){ 
    var total = 0; 
    for(var i = 0; i < $scope.cartItems.length; i++){ 
     var item = $scope.cartItems[i]; 
     total += item.itemPrice ; 
    } 
    return total; 
}; 

    } 
]); 

回答

0

我實現了一個很小的一部分,只是爲了讓你開始朝着正確的方向: https://plnkr.co/edit/SDO5BlNfI9okDuwe4jrl?p=preview

var maxNumberOfProducts = 10; 
var currentNumberOfProducts = 0; 

的IDEEA是在服務中存儲的產品數量,你可以有和數量您購物車中的產品。當用戶嘗試添加新產品時,您只需驗證產品數量是否超過購物車容量。 如果您有任何問題,請隨時詢問。

+0

喜明白你的邏輯,但在那裏,我的意思是比如用戶給產品的數量,並且應該得到體現在cartdetails頁的路要走,如果他能2作爲iPhone的數量,在車的詳細信息頁2個產品,被添加? – vennapusa

+0

在這裏你可以:https://plnkr.co/edit/if89lOuYxHY7OYBRPG9q?p=preview – robert

相關問題