我用来缩短字符串的自定义管道不起作用。我已将它包含在我的 app.module 声明中并将其导入到我的组件中。代码如下。
` import { Pipe, PipeTransform } from '@angular/core';
/*
Takes a string and replaces it with a shortend version based on the length you give it if its greater than 14 char for Example
someString = "hey whats up my name is Bob and im from Bob town"
{{value | shortString : length of new string}}
{{someString | shortString: 10}}
*/
@Pipe({name: 'shortString'})
export class shortString implements PipeTransform {
transform(value: any, length: number): string {
console.log('expected new string '+value.slice(0,length)+'...');
return (value.length()>14)?value.slice(0,length)+'...': value;
}
}`
首先在你的管道中
@Pipe({name: 'shortString'})
export class shortString implements PipeTransform {
transform(value: any, length: number): string {
return (value.length()>14)?value.slice(0,length)+'...': value; // remove the length() it will be value.length
}
}`
相同的工作plunker
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句