不能在ng-repeat循环中使用过滤器

时间:2015-06-01 11:55:46

标签: angularjs angularjs-ng-repeat

是否可以将过滤器应用于ng-repeat内的值?我试图在循环中剪切长字符串,但Angular忽略了我的过滤器。没有结果。没有错误。没有。为什么呢?

<tr ng-repeat="entry in event.log track by $index">
    <td>{{entry.raiseDate}}</td>
    <td>{{entry.text | cut}}</td>
    <td>{{entry.sign}}</td>
</tr>

angular.module('app.event')

.filter('cut', [function cut() {
    console.debug('im here');  // never called
    return function(input) {
        console.debug(input);
        var result;
        if ( input.length > 100 ) {
            result = input.substring(0, 100) + '...';
        } else {
            result = input;
        }
        return input;
    };
}]);

我也尝试了预定义的&#34;小写&#34;过滤器,它也不起作用

2 个答案:

答案 0 :(得分:0)

<div class="content"> <div class="container-fluid header"> <div class="container"> <div class="row"> <!-- SOME HTML CODE HERE--> </div> </div> </div> <div style="clear: both;"></div> <iframe src="/game/" width="100%" height="786" style="border: none" id="table-1"></iframe> </div> 语法错误,应为.filter('cut', [function cut() {.filter('cut', ['cut', function(cut) {

检查filter doc

请尝试以下代码:

.filter('cut', function() {

.filter('cut', ['cut', function(cut) {
    console.debug('im here');  // never called
    return function(input) {
        console.debug(input);
        var result;
        if ( input.length > 100 ) {
            result = input.substring(0, 100) + '...';
        } else {
            result = input;
        }
        return input;
    };
}]);

答案 1 :(得分:0)

可以将过滤器应用于ng-repeat

中的值

中尝试以下代码
 <td>{{entry.text | limitTo: 100 }} {{entry.text > 100 ? '...' : ''}}</td>
相关问题