Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a $scope variable inside a filter

Tags:

angularjs

I need to change a $scope variable inside a filter. The $scope variable is used for a ng-show attribute and the information is only acceded in the filter because I have a ng-repeat with some information and applied by some filters and I need to know when the filters delete all my result to show a message... here is an example: (this is only an idea)

.controller("thing", function() {
   $scope.showText = false;
})

.filter("filterText", function() {
   return function(information) {
     if (information == "") { /* NEED TO CHANGE $scope.showText to true */ }
   }
})

HTML:

<div ng-view="showText"> Some Text here </div>
<div ng-repeat="info in information | filterText"></div>

Thanks.

like image 804
Ivan Xavier Santirachi Avatar asked Jun 14 '13 20:06

Ivan Xavier Santirachi


1 Answers

I agree with the comments that you probably don't want to be changing data in the filter in the first place but if you were really hard pressed you might be able to achieve this by just defining a filter function inside your controller (rather than an actual angular "filter") and then just use it as such:

ng-repeat="item in items | filter:myFilter()"

$scope.myFilter = function(item) {
    // access to scope here
}
like image 198
Jeff Swensen Avatar answered Nov 15 '22 04:11

Jeff Swensen