I am creating a custom filter for numbers in Angular 1.5 and I need to use the existing number filter in it. I have this number format 3445 and I need to convert it to 3'445. For this purpose, first, I need to pass the input number to the number filter,

3445.05252 -> 3,445.1 -> 3'445.1

The problem is that I don't know how to use the number filter in my filter function. Here is my code:

 app.filter('tick', function () {
      return function(input, $number) {
        var number =$number(input,1);
        return number.toString().replace(",","'");
};

});

I can not use it like 3445.05252 | number:1 | tick. It must be with the number filter inside my filter.

here it is with $filter injected:

app.filter('tick', function () {
return function(input, $filter) {
        var number = $filter('number')(input,1);
    return number.toString().replace(",","'");
};

});

share|improve this question
1  
you tried inject $filter service? and use $filter('number')? – Jesús Quintana 29 mins ago

You can apply multiple filters in angularJS using piping.

Instead of trying to inject $number into your function, just apply both filters to your data in the HTML.

e.g. 3445.05252 | number | thick

This will apply the number filter, then pass the result of that into your thick function.

Result: 3'445.1

share|improve this answer
    
I have edited the question – mn93 16 mins ago
    
I have posted another answer – Brandon Ibbotson 2 mins ago

The poster has updated the question so I'm adding another answer.

You can inject $filter into your own filter. This will allow you to retrieve the number filter by name and use it however you like.

Example:

app.filter('tick', function (input, $filter) {
    var numberFilter = $filter('number');
    var filteredInput = numberFilter(input, 1);
    return filteredInput.toString().replace(",", "'");
});

Then in the HTML you can just say... 3445.05252 | tick

share
    
When I try <p> {{ 245258.222|tick}} </p> , I also get {{ 245258.222|tick}} . Something is not working. – mn93 2 mins ago
    
Are any errors being logged to the console? – Brandon Ibbotson 33 secs ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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