0

I have an ngRepeat directive that is filtered as:

<li ng-repeat='item in items | filter:searchTerm as filteredItems'>{{item.name}}</li>

I am tring to access the filteredItems in my controller like $scope.filteredItems but I'm getting an undefined. How should I solve this?

I have nested controllers. Is the problem with $scope?

My controllers are something like this in my view:

<div ng-controller='MainController'>
 <div ng-controller='FilterController'>
  <li ng-repeat='item in items | filter:searchTerm as filteredItems'>{{item.name}}</li>
 </div>
</div>

I'm trying to access $scope.filteredItems in FilterController.

12
  • Will you please share more code ? Commented Jul 9, 2015 at 16:56
  • Which part would you need? Commented Jul 9, 2015 at 16:57
  • Nested controllers ?? Commented Jul 9, 2015 at 16:57
  • 3
    you can try <li ng-repeat='item in (filteredItems = items | filter:searchTerm)'>{{item.name}}</li>, but not sure that this works, also this code can add filteredItems not in main scope, but in ngRepeat own scope Commented Jul 9, 2015 at 16:58
  • @Grundy Exactly. That's why we need to see more code to answer proper. Commented Jul 9, 2015 at 16:59

1 Answer 1

1

You can use assigning in ng-repeat expression like ng-repeat='item in filteredItems = (items | filter:searchTerm)'
NOTE: in comment was wrong variant, because assigning more priority than pipe.

var appModule = angular.module('app', []);
appModule.controller('MainController', function ($scope) {
    //some functions and variables here
    $scope.items = [
        {name:'1234'},
        {name:'2341'},
        {name:'3412'},
        {name:'4123'},
        {name:'4321'}
    ];
});
appModule.controller('FilterController', function ($scope) {
    
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.js"></script>
<div ng-app="app" ng-controller='MainController'>
    <div ng-controller='FilterController'>
        <!-- FilterController has many filters that are not related -->
        <input type="text" ng-model="searchTerm" />
        <ul>
            <li ng-repeat='item in filteredItems = (items | filter:searchTerm)'>{{item.name}}</li>
        </ul>
        {{filteredItems}}
    </div>
</div>

but seems that better filter in controller:

var appModule = angular.module('app', []);
appModule.controller('MainController', function ($scope) {
    //some functions and variables here
    $scope.items = [
        {name:'1234'},
        {name:'2341'},
        {name:'3412'},
        {name:'4123'},
        {name:'4321'}
    ];
});
appModule.controller('FilterController', function ($scope,$filter) {
    var filter = $filter('filter');
    $scope.filter = function(){
      $scope.filteredItems = filter($scope.items, $scope.searchTerm);
    }
    $scope.filter();
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.js"></script>
<div ng-app="app" ng-controller='MainController'>
    <div ng-controller='FilterController'>
        <!-- FilterController has many filters that are not related -->
        <input type="text" ng-model="searchTerm" ng-change="filter()" />
        <ul>
            <li ng-repeat='item in filteredItems'>{{item.name}}</li>
        </ul>
        {{filteredItems}}
    </div>
</div>

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

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.