If you are dealing exclusively with the "0" indentation and do not mind sewing your filters using a use case, I would go with something like an endless answer for speed, but I would recommend that you make sure that the number is not long enough with something like:
app.filter('minLength', function () { return function(input,len){ input = input.toString(); if(input.length >= len) return input; else return("000000"+input).slice(-len); } });
Since this will not only save it from trimming numbers or strings that already satisfy the minimum length, which is important in order to avoid such strange things as:
{{ 0.23415336 | minLength:4 }} //Returns "0.23415336" instead of "5336" like in Endless code
But using "000000" instead of a number such as 1e6, you avoid both changing the actual value of the input (without adding 1,000,000 to it), and avoiding the need to implicitly convert the number to a string, thereby preserving the computational step given that the input has already been converted to line to avoid the clipping problem mentioned above.
If you want a system that does not need to be tested for use, which is faster and more flexible than the bguiz solution, I use a filter, for example:
app.filter('minLength', function(){ return function(input, len, pad){ input = input.toString(); if(input.length >= len) return input; else{ pad = (pad || 0).toString(); return new Array(1 + len - input.length).join(pad) + input; } }; });
This allows you to fulfill the standard:
{{ 22 | minLength:4 }} //Returns "0022"
But it also gives you the ability to add non-zero fill parameters, for example:
{{ 22 | minLength:4:"-" }} //Returns "--22"
and you can forcefully use stupid stuff with numbers or lines, for example:
{{ "aa" | minLength:4:" " }} //Returns " aa"
In addition, if the input signal is already longer than the desired length, the filter simply ejects it without any clipping:
{{ 1234567 | minLength:4 }} //Returns "1234567"
You also avoid the need to add validation for len , because when you call the filter without the len argument, angular will RangeError in your console on the line where you are trying to create an array of length null , which simplifies debugging.