如何在Angularjs的单个模块中创建多个自定义过滤器

如何在Angularjs的单个模块中创建多个自定义过滤器

问题描述:

我正在关注本教程,并相应地创建了自定义过滤器下方.然而,最后一个导致第一个消失.当您使用truncate过滤器时,它是一个例外.如何在一个模块中创建多个过滤器?

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