在测试了一些可用的 Jquery 插件后,这是效果最好的插件:Trunk8
原因:
- 加载速度更快。
- 适用于普通测试和 html(代码不多)。
- Costumization 可以在指令中完成。
步骤:
- 下载Trunk8
- 添加对jQuery 和插件的访问权限:trunk8.js
- 创建指令(见下文)
- 添加对指令的访问权限(如有必要)
- 从 app.js 访问 modude:var app = angular.module('app', ['ellipsis'])
- 从您的角度调用指令。
指令:
angular.module( 'ellipsis', [])
.directive('ellipsis', [function () {
return {
required: 'ngBindHtml',
restrict: 'A',
priority: 100,
link: function ($scope, element, attrs, ctrl) {
$scope.hasEllipsis = false;
$scope.$watch(element.html(), function(value) {
if (!$scope.hasEllipsis) {
// apply this code ONCE
$scope.hasEllipsis = true;
$(element).trunk8({
fill: '… <a id="read-more" href="#">read more</a>', /*(Default: '…') The string to insert in place of the omitted text. This value may include HTML.*/
lines: 3, /*(Default: 1) The number of lines of text-wrap to tolerate before truncating. This value must be an integer greater than or equal to 1.*/
//side: 'right', /*(Default: 'right') The side of the text from which to truncate. Valid values include 'center', 'left', and 'right'.*/
tooltip: false, /*(Default: true) When true, the title attribute of the targeted HTML element will be set to the original, untruncated string. Valid values include true and false.*/
//width: 'auto', /*(Default: 'auto') The width, in characters, of the desired text. When set to 'auto', trunk8 will maximize the amount of text without spilling over.*/
parseHTML: true /*(Default: 'false') When true, parse and save html structure and restore structure in the truncated text.*/
//onTruncate /*(Callback): Called after truncation is completed.*/
});
$(element).on('click', '#read-more', function (event) {
$(element).trunk8('revert').append(' <a id="read-less" href="#">read less</a>');
});
$(element).on('click', '#read-less', function (event) {
$(element).trunk8();
});
}
});
}
};
}]);
查看:
<ion-item ng-repeat="page in pages">
<div ng-bind-html="page.extract" class="item item-text-wrap" ellipsis></div>
</ion-item>