6

snapshot

I want that on click of "Add to Cart" button, item will add on "My Cart". Here is my angularJS code

app.controller("OnlineShopping", function($scope)
 { 
    $scope.items = [
        {Product:"Moto G2", Desc: "Android Phone", Price: 10999},
        {Product:"The Monk who sold his ferrari", Desc: "Motivational book", Price: 250},
        {Product:"Mi Power Bank", Desc: "Power Bank", Price: 999},
        {Product:"Dell Inspiron 3537", Desc: "Laptop", Price: 45600}
    ];
    $scope.editing = false;
    $scope.addItem = function(item) {
        $scope.items.push(item);
        $scope.item = {};
    };

I found some questions here with using ng-model and ng-bing but it works with textbox, but here I am not taking input from the textbox. Here is my incomplete code for "My Cart"

    <h2>My Cart</h2>
        <div style="border: 1px solid blue;">
        </div>
        <table border="1" class="mytable">
        <tr>
        <td>Item</td>
           <td>Description</td>
           <td>Price</td>
        </tr>
        <tr ng-repeat="item in items">
                  <!-- What should be here -->
        </tr>
       </table>
3
  • do you want us to complete your code..or you have some doubts? Commented May 12, 2015 at 21:07
  • @pankajparkar I just want to know where I am going wrong. Check kathir answers Commented May 13, 2015 at 7:17
  • I was expecting you to update your question..bdw glad to see your problem is solved Commented May 13, 2015 at 7:18

2 Answers 2

14

You just need to add the cell values using the . But before that When you click on "Add to Cart" button the item needs to be added to a different variable $scope.myCartItems

$scope.addToCart = function(item)
{
$scope.myCartItems.push(item);
}

And the template will change like below,

<h2>My Cart</h2>
        <div style="border: 1px solid blue;">
        </div>
        <table border="1" class="mytable">
        <tr>
        <td>Item</td>
           <td>Description</td>
           <td>Price</td>
        </tr>
        <tr ng-repeat="item in myCartItems">
             <td>{{item.Product}}</td>
<td>{{item.Desc}}</td>
<td>{{item.Price}}</td>
        </tr>
       </table>

Take a look at this plnkr. http://plnkr.co/edit/zW7k8J9it1NIJE3hEwWI?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

works fine after adding console.log("Adding to the cart"); console.log(item); in my angularJS code. It will be more helpful for me and others also if you can explain what does it do?
console.log statements are only for debugging purpose, they dont make any difference to the functionality of the method.
0

It looks good.

Try removing the line

$scope.item = {};

and also wrap it around a $watch

$scope.$watch('items', function(){
      console.log('items changed');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.