回答
将文本截断为角的两种方法。
let str = 'How to truncate text in angular';
1.解决方案
{{str | slice:0:6}}
输出:
how to
如果要追加片串状后的任何文本0
{{ (str.length>6)? (str | slice:0:6)+'..':(str) }}
输出:
how to...
2.解决方案
如果你想创建自定义的截断管
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'truncate'
})
export class TruncatePipe 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;
}
}
在标记
{{ str | truncate:[20] }} // or
{{ str | truncate:[20, '...'] }} // or
**我认为一个参数应给予象下面这样:** 变换( value:string,arg1:string,arg2:string):string {limit} = arg1? parseInt(arg1,10):10; let trail = arg2? arg2:'...'; 返回value.length>限制? value.substring(0,limit)+ trail:value; } – him
@him greate。更新回答 –
截断管道与可选 PARAMS:
- 限制 - 字符串最大长度
- completeWords - 国旗在最近的完整的单词截断,而不是字符
- 省略号 - 附加尾随后缀
-
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'truncate'
})
export class TruncatePipe implements PipeTransform {
transform(value: string, limit = 25, completeWords = false, ellipsis = '...') {
if (completeWords) {
limit = value.substr(0, 13).lastIndexOf(' ');
}
return `${value.substr(0, limit)}${ellipsis}`;
}
}
不要忘记添加一个模块条目。
@NgModule({
declarations: [
TruncatePipe
]
})
export class AppModule {}
使用
示例串:
public longStr = 'A really long string that needs to be truncated';
标记:
<h1>{{longStr | truncate }}</h1>
<!-- Outputs: A really long string that... -->
<h1>{{longStr | truncate : 12 }}</h1>
<!-- Outputs: A really lon... -->
<h1>{{longStr | truncate : 12 : true }}</h1>
<!-- Outputs: A really... -->
<h1>{{longStr | truncate : 12 : false : '***' }}</h1>
<!-- Outputs: A really lon*** -->
你能告诉我用''符号包装的返回值是什么吗? – ConquerorsHaki
13是什么? – user292701
我还会在'if(completeWords)'块之后加上'if(value.length <= limit){ellipsis ='';}' – yahyazini
如果你想通过一个字数的截断并添加省略号你可以使用这个功能离子:
truncate(value: string, limit: number = 40, trail: String = '…'): string {
let result = value || '';
if (value) {
const words = value.split(/\s+/);
if (words.length > Math.abs(limit)) {
if (limit < 0) {
limit *= -1;
result = trail + words.slice(words.length - limit, words.length).join(' ');
} else {
result = words.slice(0, limit).join(' ') + trail;
}
}
}
return result;
}
例子: https://github.com/yellowspot/ng2-truncate/blob/master/src/truncate-words.pipe.ts
如果你想通过几封信截断,但不要剪出来的话使用本:
truncate('Bacon ipsum dolor amet sirloin tri-tip swine', 5, '…')
> "Bacon ipsum dolor amet sirloin…"
取自:
truncate(value: string, limit = 25, completeWords = true, ellipsis = '…') {
let lastindex = limit;
if (completeWords) {
lastindex = value.substr(0, limit).lastIndexOf(' ');
}
return `${value.substr(0, limit)}${ellipsis}`;
}
实施例:
truncate('Bacon ipsum dolor amet sirloin tri-tip swine', 19, true, '…')
> "Bacon ipsum dolor…"
truncate('Bacon ipsum dolor amet sirloin tri-tip swine', 19, false, '…')
> "Bacon ipsum dolor a…"
- 1. 如何截断文本中的文本
- 2. 如何截断HTML中的文本
- 3. 如何截断html中的文本
- 4. 如何截断文本,Android的
- 5. ngTable中截断文本
- 6. 在PHP中截断文本?
- 7. 在TTStyledTextLabel中截断文本
- 8. 如何使用CSS截断文本?
- 9. 如何避免文本截断与UITableViewCellStyleValue2
- 10. 截断div内的文本
- 11. 我的文本被截断
- 12. 截断Magento的文本
- 13. UILabel被截断的文本
- 14. 与文本截断的DIV
- 15. 截断AspxGridView单元格中的文本
- 16. WPF:自动截断TextBlock中的文本
- 17. 截断在RadGrid列中的文本
- 18. 截断UIButton中的斜体文本?
- 19. ListView中的Android文本被截断
- 20. 截断sharepoint中的文本字符串
- 21. 如何根据方向“截断”android中textview中的文本?
- 22. 动态UILabel截断文本
- 23. IE截断文本右侧
- 24. 使用linq截断文本
- 25. Multiline mx:文本截断
- 26. PHP截断文本base64
- 27. Rails:智能文本截断
- 28. MySQL文本列被截断
- 29. 多行文本截断
- 30. 文本溢出CSS截断
有用的角2管[这里](https://github.com/fknop/angular-pipes) –