回答
將文本截斷爲角的兩種方法。
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) –