0

I have this expression in the view:

<div ng-repeat="friend in (filtered = (friendsData | matchnames:search.pattern)) | myLimitTo : 9 : pageIndex * 9"" >

I do the filtered so i can show something like this

{{filtered.length}} 

in the view which works great, but how can i access this param in the controller so i do stuff with it?

5
  • this one shows me undefined Aug 5, 2015 at 7:04
  • @totothegreat try this plnkr plnkr.co/edit/AFT7ymvNiioCg4bH4B3j?p=info let me know if it works out for you.
    – Rebornix
    Aug 5, 2015 at 7:08
  • 1
    If you didn't pre-define the filtered variable in your controller, it's created in ng-repeat's own scope. If you declare filtered like $scope.filtered = []; in your controller, i think it should work..
    – Guinn
    Aug 5, 2015 at 7:12
  • @Rebornix He wants to access filtered, not the 'friend' in his example, or 'person' in your plnkr.
    – Guinn
    Aug 5, 2015 at 7:13
  • That plunker did it, i should have used the "this" and apply the changes on it, thx man! Aug 5, 2015 at 7:15

1 Answer 1

5

You can't access $scope.filtered because ng-repeat creates a new scope and sets filtered to it.

Technically you can create $scope.viewData = {} in your controller and use the following code in the template:

<div ng-repeat="friend in (viewData.filtered = (friendsData | matchnames:search.pattern)) | myLimitTo : 9 : pageIndex * 9"" >

There are some other possible ways to do the similar thing: eg if you use controllerAs you could set filtered array to it.

But in general all these tricks are not very good practice because you want to pass variable implicitly to controller and your angular expression looks quite complex.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.