Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use a filter vs a directive in Angularjs

It's a simple question - and it may have been asked (just couldn't find it..)

When would you use a filter over a directive when it comes to manipulating the data, or vice versa?

In a really really simple example, see this Plunkr

Essentially, I have the following javascript

var app = angular.module('app', []);

app.controller('MyCtrl', ['$scope', function($scope){
  $scope.testMessage = 'Some Text'
}]);

app.directive('myDirective', function(){
    return{ 
        restrict: 'A',
        link: function(scope, element, attrs){

            // do some stuff with the data
            // 
            element.html(scope.testMessage + ' result of my directive');
        }
    }
});

app.filter('myFilter', function(){
    return function(text){
     // do something with text
        return text + ' & result of my filter';
    }
});

And the following html

  <body ng-controller="MyCtrl">
    <div my-directive ng-model="testMessage" ></div>
    <br />
    <div>{{ testMessage | myFilter }}</div>
  </body>

So when would you use one over the other?

like image 345
Darren Wainwright Avatar asked Feb 23 '14 21:02

Darren Wainwright


People also ask

What is the use of filter in AngularJS?

AngularJS Filters allow us to format the data to display on UI without changing original format. Filters can be used with an expression or directives using pipe | sign. Angular includes various filters to format data of different data types.

Why filter is used in Angular?

Filter is an important part in AngularJS as well as Angular 2 or Angular 4. It is basically used to filter an item from a group of items, which are there in an array or an object array. It selects a subset of the items from an array and returns it as a new array and this item is displayed on UI.

Why do we use directives in AngularJS?

Directives are markers on a DOM element that tell AngularJS to attach a specified behavior to that DOM element or even transform the DOM element and its children. In short, it extends the HTML. Most of the directives in AngularJS are starting with ng- where ng stands for Angular.

Which one is correct way to apply filters in AngularJS?

In AngularJS, you can also inject the $filter service within the controller and can use it with the following syntax for filter. Syntax: $filter("filter")(array, expression, compare, propertyKey) function myCtrl($scope, $filter) { $scope. finalResult = $filter("filter")( $scope.


1 Answers

Some hints (non-exhaustive lists):

Directive

Use it when...

  • You want to do structural manipulation of the DOM
  • You want to add behaviour (the controller)
  • The outcome is dependent on other collaborators (the require configuration of the directive)

Filter

Use it when...

  • You are transforming a value to another (e.g. String → Date)
  • (Subset of the above, but important to warrant it's own bullet:) When you are transforming an array (e.g. to be displayed in repeated elements - the filter filter and ng-repeat)
like image 111
Nikos Paraskevopoulos Avatar answered Oct 01 '22 13:10

Nikos Paraskevopoulos