10

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);  
    };
});

4 Answers 4

18
angular.module('filters', []).filter("truncate", function() {})
                             .filter("escape", function() {});
Sign up to request clarification or add additional context in comments.

2 Comments

How would you do the same but with a separate file for each filter?
Sorry for answering my own question, I guess it would be: angular.module('filters', []) angular.module('filters').filter("truncate", function() {}); angular.module('filters').filter("escape", function() {});
4

The second one should be declared using

angular.module('filters').filter(....)

syntax.

The syntax you are using creates a new module everytime.

6 Comments

Thanks that solved it. It would have been nice they mentioned that somewhere. I couldn't find it anywhere.
Actually it is there in developer guide docs.angularjs.org/guide/module under Create vs Retrieval section
I think you only would know that page when you already know the answer. Otherwise you're just thinking wtf It should say there in docs.angularjs.org/guide/module or an example where they make two filters
Agreed, but this should also make you inquisitive about what is this syntax about,and hence explore the documentation :)
I did, for 1.5 hour :(
|
1

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) {
    ...
  };
});  

Comments

0
angular.module('filters')
  .filter({
    groupBy: ['$parse', groupByFilter],
    countBy: [countByFilter]
  });

//function to handle the filter
function groupByFilter($parse) {
  return function(input) {
    ...
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.