Здравствуйте. Вот пример, который вы можете проверить.
App.module.ts
import { NgModule, Component, Injectable } from '@angular/core';
import { LimitToPipe } from './pipes/limit-to.pipe'; // your pipe path
@NgModule({
declarations: [
LimitToPipe
]
});
Создать limit-to.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'limitTo'
})
export class LimitToPipe implements PipeTransform {
transform(value: string, args: string): string {
const limit = args ? parseInt(args, 10) : 10;
const trail = '...';
return value.length > limit ? value.substring(0, limit) + trail : value;
}
}
импорт в ваш компонент
import { LimitToPipe } from '../pipes/limit-to.pipe';
в вашем html
{{ name | limitTo :60 : 0 }}
Юнит-тест (limit-to.pipe.spec.ts)
import { LimitToPipe } from './limit-to.pipe'; // your custom pipe path
describe('Pipe: LimitTo', () => {
let pipe: LimitToPipe;
beforeEach(() => {
pipe = new LimitToPipe();
});
it('show 3 dot when string length is grater ther 15 chartacter', () => {
expect(pipe.transform('my name is vikram sharma', '15')).toBe('my name is vikr...');
});
it('show full string when string length is less then 15 character', () => {
expect(pipe.transform('my name is vikram sharma', '200')).toBe('my name is vikram sharma');
});
});