truncate-pipe.ts
Angular 2+
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'truncate' }) export class TruncatePipe implements PipeTransform { transform(value: string, len: number, typeLen: string): any { /** * check null & undefine */ console.log('value: ' + value); console.log('len: ' + len); console.log('typeLen: ' + typeLen); /** * custom pipes */ return value + '...'; } }
app.module.ts
Angular 2+
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { TruncatePipe } from './truncate-pipe'; @NgModule({ declarations: [ AppComponent, TruncatePipe ], imports: [ BrowserModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
app.component.ts
Angular 2+
import { Component } from '@angular/core'; import { TruncatePipe } from './truncate-pipe'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], providers: [TruncatePipe] }) export class AppComponent { test = 'The Dolore is a French river in the Auvergne region.'; }
app.component.html
Angular 2+
<h2>{{ test | truncate : 15 : 'fullwidth' }}</h2>
0 nhận xét:
Post a Comment