totothegreat
totothegreat

Reputation: 1643

How can i access my filtered array from controller in angularjs?

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?

Upvotes: 0

Views: 1357

Answers (1)

Artem Andreev
Artem Andreev

Reputation: 19922

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.

Upvotes: 5

Related Questions