如何在AngularJS中使用过滤器进行ng-repeat循环?

时间:2017-06-26 18:29:48

标签: javascript angularjs arrays

我正在进行ng-repeat,它将填充来自碰撞对象的div我在那些具有较大值的对象中有一个名为videos.description的字段,因此我想将其值更改为短值使用过滤器我的代码:

<div class="panel panel-default videoPanel" ng-repeat="video in videos.data | shortDescription :'video.description'">
    <h6 style="text-align: center">{{video.name}}</h6>
    <div class="panel-body" style="margin-top:-50px;">
        <video width="100%" height="190" controls style="margin:auto">
            <source src="test.mp4" type="video/mp4"> Your browser does not support the video tag.
        </video>
        <input id="input-7-xs" class="rating rating-loading" value="1" data-min="0" data-max="5" data-step="0.5" data-size="xs">
        <p>{{video.description}}</p>
    </div>
</div>

这是我的过滤器:

app.filter('shortDescription', function() {
  return function(items, field) {
        var result = [];
        length=40;
        angular.forEach(items, function(value, key) {
            items[key].value=value.description.substring(0, length);
            console.log(field);
            result.push(items[key]);
        });
        return result;
    };
});

使用此过滤器我仍然在字段描述中有一个很大的值,有人可以帮助我提前感谢

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

items[key].value=value.description.substring(0, length);

您必须更新description

items[key].description=value.description.substring(0, length);

对于此功能,您可以使用map方法,该方法接受callback功能。

return items.map(function(item){
   return {"name":item.name,"description":item.description.substring(0,length)};
});