Lakshmi Narasimhan
Lakshmi Narasimhan

Reputation: 159

How to prevent same item being added to cart?

Im new to angular js i want to know how to prevent user from adding same item more than once to cart . i have developed a simple cart application i have attached sample screenshot below Screen 1

screen 1

Screen 2

Screen 2

after clicking the data will be passed to an array.How can i prevent user from adding same item more than once i have used ng-repeat to retriecve data from json file.

Upvotes: 1

Views: 619

Answers (1)

Ruhul
Ruhul

Reputation: 1030

I think you should achieve this using logic similar to below;

    $scope.addProduct = function(product){
    var isDuplicate = false;
    for(var i=o;i<$scope.selectedProduct.length;i++){
       if(product.id == $scope.selectedProduct[i].id){
        isDuplicate =true;
         break;
        }

   }
   if(!isDuplicate ){
        $scope.selectedProduct.push(product)


     }
 }

Upvotes: 1

Related Questions