Angular.js filter premade html element

I was playing with angular.js the other day and I found this filter function, that angular.js provides for us.

<div class="container-fluid">
  <div class="row-fluid">
    <div class="span2">
      <!--Sidebar content-->

      Search: <input ng-model="query">

    </div>
    <div class="span10">
      <!--Body content-->

      <ul class="phones">
        <li ng-repeat="phone in phones | filter:query">
          {{phone.name}}
          <p>{{phone.snippet}}</p>
        </li>
      </ul>

    </div>
  </div>
</div>

My question is: Can I use angular.js filter on premade html elements, somehing like this.

<div class="container-fluid">
  <div class="row-fluid">
    <div class="span2">
      <!--Sidebar content-->

      Search: <input ng-model="query">

    </div>
    <div class="span10">
      <!--Body content-->

      <ul class="phones" ng-filter:query>
        <li>First element</li>
        <li>Second elementy/li>
        <li>Third element</li>
      </ul>

    </div>
  </div>
</div>

Thank you for your answers!

No. Your first example uses a filter - called "filter"! A filter can form part of an Angular binding expression. It is placed after a pipe character, and applies a "filter function" to the part of the expression that came before the pipe. Some filters also take additional parameters, to the right of a colon. The filter called "filter" acts on an Array (the part before the pipe, in this case phones) passing each item through a check determined by the parameter to the right of the colon. In your case, using a string variable called query, it returns an Array with any items from phones that contain the string in query.

Other examples of filters in Angular include currency, date, uppercase and orderBy. They all take an input (for example a string) returning another value (for example the uppercase version of the string) and in some cases additional configuration parameters (such as a date or currency format, or field to order by). But they only work with an input that is some value in the "data model", not directly on the content of a DOM node.

Your second example attempts to use a directive called "ngFilter". A directive is an extension to standard HTML syntax, and can be expressed as hyphenated attributes (as in this case), data- attributes (data-ng-filter), namespaced attributes (ng:filter), css classes, etc. Angular's default directives have the prefix "ng". But there is no such directive as "ngFilter" in Angular. Your example will load fine, but there will be no effect on the DOM processing from adding this non-existent directive.

For this kind of DOM manipulation/filtering, Angular directives ngSwitch or ngShow/ngHide are normally used.

<ul class="phones" ng-switch on="query">
<li ng-switch-when="Nexus S">First element</li>

The above would look for an exact match though (so it is not as nice as @tosh's directive). ng-switch is often used with a select drop-down, where the possible values are fixed/known.

ngShow/ngHide are probably a better match for what you are trying to do. An in-line expression or $scope function can be used to determine whether to show an element:

<li ngShow="some expression using query">First element</li>
<li ngShow="myFilter()">First element</li>

function MyCtrl($scope) {
    $scope.myFilter = function() {
       if($scope.query ...) {  // could use RegExp() here like @tosh
          return true
       }
       return false
    }

The above does not require jQuery.

I do not think that is part of the default directive, but that's interesting task.

I tried to implement with a custom directive. http://plnkr.co/edit/TOGbtq

app.directive('ngFilter', function() {
  return {
    link: function(scope, element, attr) {
      scope.$watch(attr.ngFilter, function(q){
        $(element).children().each(function(i,a){
          $(a).toggle((new RegExp(q)).test($(a).text()));
        });
      });
    }
  };
});