Last Updated: February 25, 2016
· glebm

angularjs where filter

Update The latest release of angular adds a comparator argument to the filter filter, which can be used to achieve what the code below does (just pass true, see

Often we need to filter objects by specific attributes. Angularjs comes with a limited filter filter, but we can write our own:

module.filter 'where', ->
  matchProperty = (obj, key, shouldEqual) ->
    value = obj[key]
    value = if angular.isFunction(value)
    value == shouldEqual

  (input, properties) ->
    r = input.slice(0)
    for k, v of properties
      i = 0
      while i < r.length
        if matchProperty(r[i], k, v) then i++ else r.splice(i, 1)
      return r unless r.length

Now we can filter:

li ng-repeat="l in locations | where:{type:'country'}"

1 Response
Add your response


@glebby, you can write input[...] instead of input.slice(0)

I miss you,

over 1 year ago ·