Skip to content

Instantly share code, notes, and snippets.

@chl03ks
Created August 11, 2016 15:13
Show Gist options
  • Save chl03ks/c7fbd8b7f632f5891e98854b024f0557 to your computer and use it in GitHub Desktop.
Save chl03ks/c7fbd8b7f632f5891e98854b024f0557 to your computer and use it in GitHub Desktop.
An Angular 2 pipe to capitalize the first letter of a string value.
import { Pipe, PipeTransform } from '@angular/core';
/*
* Capitalize the first letter of the string
* Takes a string as a value.
* Usage:
* value | capitalizefirst
* Example:
* // value.name = daniel
* {{ value.name | capitalizefirst }}
* fromats to: Daniel
*/
@Pipe({
name: 'capitalizeFirst'
})
export class CapitalizeFirstPipe implements PipeTransform {
transform(value: string, args: any[]): string {
if (value === null) return 'Not assigned';
return value.charAt(0).toUpperCase() + value.slice(1);
}
}
@wBatdeveloppement4
Copy link

Line 17:
Use
if (!value) return null;
Instead of
if (value === null) return 'Not assigned';

Nope, that would empty the 0 input.
Use

if (value === null || value === undefined) {
            return '';
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment