Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi -

In my controller I am looping through my data result from a service call. My object has a property of storeID. If that ID is greater than 0 I am trying to set a disabledToggle to true otherwise the variable to false. What is happening is all items are either true or all items are false. Here is the code.

HTML

HTML
<tr ng-repeat="item in vm.items track by $index" style="removed:pointer">
     <td ng-bind="(item.itemNo | uppercase)"> </td>
     <td ng-bind="(item.description | uppercase)"></td>
     <td align="right" ng-bind="(item.listPriceDisplay | number:2)"></td>
      <td>
         <a ng-click="#>fa fa-pencil fa-2x"></a>
         <a ng-click="disabledToggled|| vm.removeItem(item)">^__i class="fa fa-trash fa-2x link-icon"></a>
      </td>
 </tr>


JavaScript
function myItems() {
    var items;


    if (vm.fetchCriteria != null) {
        vm.busy = true;
        inventoryDataService.getInventoryItems(vm.Criteria)
    .then(function (result) {
        vm.data = result.data;
        if (vm.data.length == 0) {
            return;
        } else {
            vm.fetchCriteria.skip += result.data.length;
        }
        items = vm.data;
        for (var i = 0; i < vm.data.length; i++) {
            if (items[i].storeID > 0) {
                $scope.disabledToggled = true;
            }
            else {
                $scope.disabledToggled = false;
            }
            vm.items.push(items[i]);
        }
    });
    }
}


What I have tried:

ng-click expression item.storeID > 0

I started with this article to http://jaketrent.com/post/disable-ng-click/
Posted
Updated 7-Jun-16 20:44pm

1 solution

Hey Troy Bryant,

I have gone through your code and i see a discripancy which is obvious to cause the problem.

Let me explain you the problem:-

If you can see implementation of yours, you are updating a scope variable named "disabledToggled" by checking the storeId>0.
Even though you have multiple items and storeID checkings but there is only one instance of scope variable "disabledToggled", so whenever the updates will happen inside loop the same scope varibale will get updated and only the last update will be available to the scope variable.

What you can do is add a new attribute for the item object so that it will also be retrieved with the result.data and will be available for each item in the for loop, then do like below:-

JavaScript
items[i].disabledToggled = true/false;


and also in html the change will be like:

HTML
<a ng-click="item.disabledToggled|| vm.removeItem(item)"></a>



Hope you got my point.

Please let me know if any further help is needed.
 
Share this answer
 
Comments
Troy Bryant 8-Jun-16 9:40am    
Thank you never thought about if from that way. Worked like a charm.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900