CSS
.wrap-text {
width: 250px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
HTML
<h4 class="wrap-text">Sachin Ramesh Tendulkar is a former Indian international cricketer and a former captain of the Indian national team.</h4>
从去年修剪文本最好的方法是创建自定义管,并使用它。
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'trimLast'
})
export class TrimLastPipe implements PipeTransform {
transform(value: string, args: string[]): string {
const limit = args.length > 0 ? parseInt(args[0], 10) : 20;
const trail = args.length > 1 ? args[1] : '...';
return value.length > limit ? value.substring(0, limit) + trail : value;
}
}
然后注册此自定义管道模块。您可以在整个应用程序中使用它。