129

I am using an ng-repeat directive with filter like so:

ng-repeat="item in items | orderBy:'order_prop' | filter:query | limitTo:4"

and I can see the rendered results fine; now I want to run some logic on that result in my controller. The question is how can I grab the result items reference?

Update:


Just to clarify: I'm trying to create an auto complete, I have this input:

<input id="queryInput" ng-model="query" type="text" size="30" placeholder="Enter query">

and then the filtered results:

<ul>
   <li  ng-repeat="item in items | orderBy:'order_prop' | filter:query | limitTo:4">{{item.name}}</li>
</ul>

now I want to navigate the result and select one of the items.

6 Answers 6

274

UPDATE: Here's an easier way than what was there before.

 <input ng-model="query">
 <div ng-repeat="item in (filteredItems = (items | orderBy:'order_prop' | filter:query | limitTo:4))">
   {{item}}
 </div>

Then $scope.filteredItems is accessible.

12
  • Thanks for the reply, please see my update. How would you implement that, note that I get the entire items list on init Jul 30, 2012 at 14:17
  • super cool, thanks for that. I wish it was a built in feature of NG Jul 30, 2012 at 18:00
  • 3
    The problem with this approach is that if you watch the assigned property, you'll end up spamming $digests (one per iteration)... Maybe there's some way to avoid this? Dec 24, 2013 at 12:17
  • 1
    If I watch the search object and console.log filteredItems I always get the data one filter later. It's not the result of the just changed filter, but the one from the previous change. Any ideas? Aug 3, 2015 at 15:03
  • 4
    Can someone explain me, why does this work? I can understand that a variable from the $scope, is accessible in the repeat scope (scope inheritance), but the other way around? How? Some documentation is appreciated. Thanks Sep 22, 2015 at 9:11
30

Here's the filter version of Andy Joslin's solution.

Update: BREAKING CHANGE. As of version 1.3.0-beta.19 (this commit) filters do not have a context and this will be bound to the global scope. You can either pass the context as an argument or use the new aliasing syntax in ngRepeat, 1.3.0-beta.17+.

// pre 1.3.0-beta.19
yourModule.filter("as", function($parse) {
  return function(value, path) {
    return $parse(path).assign(this, value);
  };
});

// 1.3.0-beta.19+
yourModule.filter("as", function($parse) {
  return function(value, context, path) {
    return $parse(path).assign(context, value);
  };
});

Then in your view

<!-- pre 1.3.0-beta.19 -->
<input ng-model="query">
<div ng-repeat="item in items | orderBy:'order_prop' | filter:query | limitTo:4 | as:'filteredItems'">
 {{item}}
</div>

<!-- 1.3.0-beta.19+ -->
<input ng-model="query">
<div ng-repeat="item in items | orderBy:'order_prop' | filter:query | limitTo:4 | as:this:'filteredItems'">
 {{item}}
</div>

<!-- 1.3.0-beta.17+ ngRepeat aliasing -->
<input ng-model="query">
<div ng-repeat="item in items | orderBy:'order_prop' | filter:query | limitTo:4 as filteredItems">
 {{item}}
</div>

Which gives you access to $scope.filteredItems.

2
  • Thanks! Could you explain how the filter code works? I'm not familiar with $parse, and the angular docs didn't help me decode how your filter works exactly.
    – Magne
    Jul 4, 2014 at 9:39
  • 2
    @Magne No problem! Please check out the update though as it may save you some pain and suffering down the road. To answer your question, in short, $parse is Angular's expression compiler.. For example, it'll take the string 'some.object.property' and return a function that allows you to either set or get a value at said path for a specified context. I'm using it to set filter results on the $scope here, specifically. I hope this answers your question. Oct 15, 2014 at 16:52
24

Try something like this, the problem with the ng-repeat is that it creates child scope because of that you can't access

filteritems

from the controller

<li ng-repeat="doc in $parent.filteritems = (docs | filter:searchitems)" ></li>
17

Update:

Even after 1.3.0. if you want to put it on the scope of the controller or the parent you cannot do that with as syntax. For example if I have the following code:

<div>{{labelResults}}</div>
<li ng-repeat="label in (labels | filter:query | as labelResults)">
</div>

the above will not work. The way to go around it is using the $parent as so:

<li ng-repeat="label in ($parent.labelResults = (labels | filter:query))">
2
  • It's good to note that if you have a limitTo filter on this. It will not be reflected in the $parent.labelResults
    – Zack
    Dec 19, 2016 at 19:04
  • If I console.log labelResults I always get the data one filter later. It's not the result of the just changed filter, but the one from the previous change. Didn't you have this problem?
    – Anna
    May 17, 2017 at 9:01
10

I came up with a somewhat better version of Andy's solution. In his solution ng-repeat places a watch on the expression that contains the assignment. Each digest loop will evaluate that expression and assign the result to the scope variable.

The problem with this solution is that you might run into assignment issues if you are in a child scope. This is the same reason why you should have a dot in ng-model.

The other thing I don't like about this solution is that it buries the definition of the filtered array somewhere in the view markup. If it is used in multiple places in your view or your controller it can get confusing.

A simpler solution is to just place a watch in your controller on a function that makes the assignment:

$scope.$watch(function () {
    $scope.filteredItems = $scope.$eval("items | orderBy:'order_prop' | filter:query | limitTo:4");
});

This function will be evaluated during each digest cycle so performance should be comparable with Andy's solution. You can also add any number of assignments in the function to keep them all in one place rather than scattered about the view.

In the view, you would just use the filtered list directly:

<ul>
    <li  ng-repeat="item in filteredItems">{{item.name}}</li>
</ul>

Here's a fiddle where this is shown in a more complicated scenario.

2
  • Super clean and doesn't pollute the structure, nice!
    – kuzyn
    Mar 8, 2016 at 16:17
  • this solution is perfect for me..i was using ionic's collection repeat instead of ng repeat..thats why the accepted answer didnt work for me Oct 13, 2016 at 10:40
-2

Check this answer:

Selecting and accessing items in ng-repeat

<li ng-repeat="item in ..." ng-click="select_item(item)">

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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