0

I have a list of items coming from database and the items are selected by the user and as per their selection they can add the quantity of selected item by just incrementing or decrementing the counter onClick of the signs(+,-).And i have done it this way

 <table   class="table table-hover"><tr ng-repeat="samData in sampleData">
<td >{{samData}}</td>
<td> <a ng-click="increment()">+ {{count}}</a>
  <a ng-click="decrement()" >-</a>
</td><td>

controller js:

 $scope.decrement = function() {
             $scope.count = $scope.count - 1;
            if ($scope.count < 0){
              $scope.count = 0;
           }
   };
      $scope.increment = function() {
       $scope.count = $scope.count + 1;
};

but the problem is as i click on any particular "td" all the counters gets affected which i don't want plz tel me how to associate the counter to each record and accordingly make the changes to the only clicked 'td'

2
  • count should be a property of samData e.g. samData.count ?? Are you missing anything here. Commented Aug 5, 2014 at 10:36
  • Add a counter to each sampleData and pass it as a parameter to increment and decrement Commented Aug 5, 2014 at 10:37

3 Answers 3

2

Try moving your controller inside your ng-repeat:

<table   class="table table-hover"><tr ng-repeat="samData in sampleData">
<td >{{samData}}</td>
<td ng-controller="controller"> <a ng-click="increment()">+ {{count}}</a>
  <a ng-click="decrement()" >-</a>
</td><td>
Sign up to request clarification or add additional context in comments.

2 Comments

Right, but count wouldn't be available to the parent controller correct?
That's correct, but as far as I can see, it doesn't need to be. count is only referenced inside the controller. By placing the ng-controller inside of the ng-repeat, each iteration will have its own controller scope, and hence its own 'count' variable on that scope.
1

that's normal, you have a count for all your samdata, you need a particular one for each

try it

<td ng-init="samData.count = 0"> <a ng-click="samData.count = samData.count + 1">+      {{samData.count}}</a>
<a ng-click="samData.count = samData.count - 1" ng-if="samData.count > 0">-</a>

Comments

0

Don't forget your $scope variable can be a JSON object. So use a value and count attribute.

You can also pass in the current value from an ng-repeat and it is used as a pointer to the record in the row.

html

<table  class="table table-hover">
    <tr ng-repeat="samData in sampleData">
        <td>{{samData.value}}</td>
        <td><a ng-click="increment(samData)">+ {{samData.count}}</a>
            <a ng-click="decrement(samData)" >-</a></td>
    </tr>
<table>

controller js

$scope.decrement = function(samData) {
     if(sameData.count > 0) {
         samData.count = samData.count - 1;
     }
}

$scope.decrement = function(samData) {
    samData.count = samData.count + 1;
}

For this implementation you would need to initialize samData.count or check for it's existence before adjusting the values

1 Comment

Shouldn't you check for existance of this count attribute, and set the attribute if it was not set before? Or you have to change the JSON object so it has that count attribute BEFORE you enter this ng-repeat.

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.