Struggling with capitalizing words in an Angular 2 template (referred to as view) led to an error in the console and the application failing to load, displaying a blank page:
Error: Uncaught (in promise): Error: Template parse errors: The pipe 'capitalize' could not be found
The objective is as follows, using examples below with a hardcoded string instead of a variable in the Angular 2 component:
- Capitalize only the first word of the sentence if no argument is provided.
{{ 'heLlo woRld' | capitalize }} // outputs "HeLlo woRld" - Only "H" is capitalized
- Capitalize all words of the string by passing the argument 'all'.
{{ 'heLlo woRld' | capitalize:'all' }} // outputs "HeLlo WoRld" - Both "H" and "W" are capitalized
- Additional edge cases to consider:
{{ 'a' | capitalize }} // outputs "A" {{ 'a' | capitalize:'all' }} // outputs "A" {{ '' | capitalize }} // outputs nothing {{ '' | capitalize:'all' }} // outputs nothing {{ null | capitalize }} // outputs nothing {{ null | capitalize:'all' }} // outputs nothing
Keep in mind that the solution should be JS-based without the use of third-party libraries like jQuery, underscore, or lodash. It should adhere to Typescript and ES6 standards.