Last active
August 21, 2016 20:36
-
-
Save carmichaelize/fb62811d2429ebff9455d127c9147d01 to your computer and use it in GitHub Desktop.
Angular2 string capitalisation pipe
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: 'capitalize' | |
}) | |
export class Capitalize implements PipeTransform { | |
transform(str: string, all: boolean) { | |
//Capitalize all the words | |
if (all) { | |
return str.split(' ').map((str) => { | |
return str.charAt(0).toUpperCase() + str.slice(1); | |
}).join(' '); | |
} | |
//Capitalize the first word only | |
return str.charAt(0).toUpperCase() + str.slice(1); | |
} | |
} |
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 { Capitalize } from './capitalize.pipe'; | |
@Component({ | |
template: ` | |
<h1>{{ title | capitalize:true }}</h1> | |
<h2>{{ subTitle }}</h2> | |
`, | |
pipes: [Capitalize] | |
}) | |
export class HelloComponent { | |
title = 'hello world title'; | |
subTitle = 'hello world sub title'; | |
constructor() { | |
this.subTitle = new Capitalize().transform(this.subTitle, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment