I'm following this tutorial and created the underneath custom filters accordingly. The last one however causes the first one to disappear; when you use the truncate
filter it an exception. How do you create multiple filters within a module?
angular.module('filters', []).filter('truncate', function () {
return function (text, length, end) {
if (isNaN(length))
length = 10;
if (end === undefined)
end = "...";
if (text.length <= length || text.length - end.length <= length) {
return text;
}
else {
return String(text).substring(0, length-end.length) + end;
}
};
});
angular.module('filters', []).filter('escape', function () {
return function(text) {
encodeURIComponent(text);
};
});
angular.module('filters', []).filter("truncate", function() {})
.filter("escape", function() {});
The second one should be declared using
angular.module('filters').filter(....)
syntax.
The syntax you are using creates a new module everytime.
One way to separate the filters out into individual files would be to create a filter module that requires the filters individually:
// init.js
angular.module('application', ['application.filters'])
// filters.js
angular.module('application.filters', [
'filters.currencyCents',
'filters.percentage',
]);
// percentage.js
angular.module('filters.percentage', []).filter('percentage', function() {
return function(decimal, sigFigs) {
...
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With