10

私はこのチュートリアルに従っており、それに応じて下のカスタム フィルターを作成しました。ただし、最後のものは最初のものを非表示にします。truncateフィルターを使用すると例外になります。モジュール内に複数のフィルターを作成するにはどうすればよいですか?

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

4 に答える 4

18
angular.module('filters', []).filter("truncate", function() {})
                             .filter("escape", function() {});
于 2013-10-01T04:25:16.013 に答える
4

2 番目のものは、次を使用して宣言する必要があります。

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

構文。

使用している構文は、毎回新しいモジュールを作成します。

于 2013-10-01T04:25:09.673 に答える
1

フィルターを個別のファイルに分離する 1 つの方法は、フィルターを個別に必要とするフィルター モジュールを作成することです。

// 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) {
    ...
  };
});  
于 2014-10-17T18:56:12.920 に答える
0
angular.module('filters')
  .filter({
    groupBy: ['$parse', groupByFilter],
    countBy: [countByFilter]
  });

//function to handle the filter
function groupByFilter($parse) {
  return function(input) {
    ...
  }
}
于 2014-09-11T08:25:45.330 に答える