Monaca Onsen UI Discord Chat Github Repo

Passing data to a function inside an ng-repeat with orderBy



  • I have an issue that I can’t seem to figure out to do with ng-repeat and orderBy. I have searched around as best I could and haven’t found anything specific on this topic.

    I have an ng-repeat on a tbody and inside that I have two tr tags. The first one just populates each row of the table with each item of the list. No problems there, works fine. The problem arises with the 2nd tr. I have an ng-if on this tr that will pass the current data item to a function and return true/false based on a check inside the function. The function lives inside the controller as a scope function. If true then it will print a new row for that data object.

    The problem is when I also apply orderyBy to the tbodys’ ng-repeat. The 1st tr is printing the data from the list as expected, in alphabetical order. But the data object when passed to the function in the 2nd tr is not the correct data object.

    Let me illustrate it with some pseudocode:

    Controller
    $scope.items = [A,C,D,A,E,A];
    $scope.sortType = ‘fieldName’;

    $scope.checkObj = function(obj) {
    console.log(obj)
    // Check obj logic
    return true;
    }

    HTML

    <tbody ng-repeat=“item in items | orderBy:sortType”>
    <tr>
    {{ item }}
    </tr>

     <tr ng-if="checkObj(item)">
        Summary: {{ item }} 
     </tr>
    

    </tbody>
    Hopefully the above code helps with my explanation. The items are ordered so the table should look like

    | A
    | A
    | A
    | C
    | D
    | E
    This works correctly, the list is correctly ordered in the ng-repeat and prints out the table in alphabetical order as Angular JS expected but the item that is passed into the checkObj function is not the one that is being printed in that iteration of the loop.

    What I mean by this is this:

    Iteration 1: Table prints out ‘A’ … checkObj prints out ‘A’ Iteration 2: Table prints out ‘A’ … checkObj prints out ‘C’ Iteration 3: Table prints out ‘A’ … checkObj prints out ‘D’ Iteration 4: Table prints out ‘C’ … checkObj prints out ‘A’ …

    For some reason the item that is being passed to the function is not the one that should be from the ordered list. It seems to be iterating over the unordered list for the ng-if and the ordered list for everything else.

    I’m stumped, if anyone has any input on this issue it would be greatly appreciated