Created
February 6, 2019 14:15
-
-
Save arkumish/7e70534f37b3ff7fa5545771deb39963 to your computer and use it in GitHub Desktop.
Angular 6 custom Pipe to get first word from string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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