Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a filter inside a filter in AngularJS?

I just create a filter to convert my date to time. And I would call in the official filter "date" of AngularJS.

project.date_created_at and project.mel don't have the same format. So I need to create a custom filter for project.date_created_at.

HTML :

<span>{{ project.date_created_at | dateCustom }}</span>
<span>{{ project.mel | date:'dd/MM/yyyy' }}</span>

JS :

myApp.filter('dateCustom', function () {
    return function (input) {

        if(input != undefined) {
            var d = new Date(input);
            var time = d.getTime();
            // use official $filter('date') here ?
        }

    }
});

I would like to use this format :

date:'dd/MM/yyyy'
like image 341
Steffi Avatar asked May 13 '14 09:05

Steffi


People also ask

Is the correct way to apply multiple filters in AngularJS?

Answer: A is the correct option. The syntax of applying multiple filters in AngularJS can be written as: {{ expression | filter1 | filter2 | ... }}

Which is the correct way to apply a filter in AngularJS?

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

Which of the following is not valid AngularJS filter *?

Which of the below is an Invalid filter in AngularJs? Explanation: The filter in angular is provide transformation of the data Email is an invalid filter in Angular JS the valid filter are JSON, limitTo, and order by.

What is correct way to apply multiple filters in AngularJS Mcq?

What of the following is the correct way for applying multiple filters in AngularJS ? Explanation: The correct syntax for applying the multiple filters is{{ expression | filter1 | filter2}}.


1 Answers

You can inject $filter as a dependency like you would do it for a controller, a service or a directive.

myApp.filter('myFilter',[ '$filter', function ($filter) {
    return function (input) {
      /**
         Do your stuff
      **/
      return $filter('date')(myDate,myFormat);
    }
}]);

On a side note, you should use angular.isDefined instead of != undefined.

See the documentation of $filter and of date for more details

like image 74
Julien Avatar answered Oct 21 '22 07:10

Julien