Skip to content

Instantly share code, notes, and snippets.

@arkumish
Created February 6, 2019 14:15
Show Gist options
  • Save arkumish/7e70534f37b3ff7fa5545771deb39963 to your computer and use it in GitHub Desktop.
Save arkumish/7e70534f37b3ff7fa5545771deb39963 to your computer and use it in GitHub Desktop.
Angular 6 custom Pipe to get first word from string
import {Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'courses',
template: `
<input type="text" [(ngModel)]="name"/>
<h2> {{name | firstWord}}</h2>
`
})
export class CoursesComponent {
name :string;
constructor() {
}
}
import { Pipe , PipeTransform } from '@angular/core';
@Pipe({
name : 'firstWord'
})
export class firstWord implements PipeTransform{
transform(value: string): string {
if (!value) { return value; }
value=value.trim();
return value.split(' ')[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment