我的 HTML 字符串存储在我的数据库中(我的新帖子的内容)。
我知道要在我的 View 中打印此 html,我可以使用此
<div ng-bind-html="post.content"></div>
这完全没问题,但假设我只想打印已解析字符串的前 20 个字符。
有没有一种方法可以存储“ng-bind-html”的返回值并仅在其上使用过滤器?
有没有更好的方法来创建 Angular 的摘录功能?
最佳答案
你可以有一个自定义过滤器:
app.filter('ellipsis', function () {
return function (text, length) {
if (text.length > length) {
return text.substr(0, length) + '...';
}
return text;
}
});
你会像这样使用它:
<div ng-bind-html="post.content | ellipsis:20"></div>
关于javascript - AngularJS 摘录函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33588300/