1

I'm trying splice items from an array but it removes the wrong item each time. I believe it is becuase I filter the array array using orderBy, so the array on the DOM is different from the array in the controller. My question now is how do I correctly splice the right item from the array even after filtering, and also is there any way I can use the orderBy filter in by controller?

Heres my controller

office.controller('notificationCtrl',['$scope',$http',function($scope,$http){

    $scope.latest = [
         {
           id:1,
           date : "2017-01-11T19:33:17.307452",
           arrived: false,
           location : "europe"
         },
         {
           id: 2,
           date: "2017-01-11T20:19:47.745673",
           arrived:false,
           location : "africa"
         }

    ]

    $scope.accept = function(array,index){
       array.splice(index,1)
    }
}]

Source.html

<div ng-repeat="recent in latest | orderBy : recent.date : true">
    <button ng-click="accept(latest,$index)">Accept</button>
</div>

EDIT I've added example data as requested, in the data above if I try to splice the second item, it spices the first instead.

2
  • Can you provide some example data? Commented Jan 11, 2017 at 19:41
  • @PritamBanerjee edited as requestd Commented Jan 11, 2017 at 19:59

3 Answers 3

1

As written here https://docs.angularjs.org/api/ng/directive/ngRepeat you can use variable in expression as alias_expression to get/store the intermediate results of the repeater, after ordering/filtering.

variable in expression as alias_expression – You can also provide an optional alias expression which will then store the intermediate results of the repeater after the filters have been applied. Typically this is used to render a special message when a filter is active on the repeater, but the filtered result set is empty.

For example: item in items | filter:x as results will store the fragment of the repeated items as results, but only after the items have been processed through the filter.

Please note that `as [variable name] is not an operator but rather a part of ngRepeat micro-syntax so it can be used only at the end (and not as operator, inside an expression).

For example: item in items | filter : x | orderBy : order | limitTo : limit as results .

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

2 Comments

If you don't mind me asking. How do I use this to solve my problem?
when you do delete from your array, you are passing $index within filtered results to NOT filtered source array. That's why it deletes the wrong element. You should ng-click="accept(alias_expression, $index)", where alias_expression is the resulting (ordered and filtered) array.
0

I think if you add the track by at the end it will work as expected. And also yo have an error in your first param, you should indicate the property with quotes like this:

<div ng-repeat="recent in latest | orderBy:'date':true track by $index">
    <button ng-click="accept($index)">Accept</button>
</div>

Your controller:

$scope.accept = function(index){
    $scope.latest.splice(index,1)
}

How to use filters in your controller:

var orderedArray= $filter('orderBy')($scope.latest, 'date');

4 Comments

I tried this it still doesnt work. Using the filter in the controller will be difficult as i'm filtering using one of the array properties. Eg. recent in latest | orderBy : 'recent.date' : true. Can this be done in the controller?
what are you trying to achieve by sending the recent?
I'm trying to order the items using the recent date
You just need to put the string 'date'
0

So I solved this by simply reversing my list like

var latest  = [
     {
       id:1,
       date : "2017-01-11T19:33:17.307452",
       arrived: false,
       location : "europe"
     },
     {
       id: 2,
       date: "2017-01-11T20:19:47.745673",
       arrived:false,
       location : "africa"
     }

]

$scope.latest = latest.reverse()

Then I removed the filter and just did ng-repeat on the list.

<div ng-repeat="recent in latest">
    <button ng-click="accept($index)">Accept</button>
</div>

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.