Skip to content

Instantly share code, notes, and snippets.

@carmichaelize
Last active August 21, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carmichaelize/fb62811d2429ebff9455d127c9147d01 to your computer and use it in GitHub Desktop.
Save carmichaelize/fb62811d2429ebff9455d127c9147d01 to your computer and use it in GitHub Desktop.
Angular2 string capitalisation pipe
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);
}
}
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