angulartypescripttitle-case

Does anyone know how to use titlecase in component.ts file instead in html file?


I tried the following code but it's not working.

import { Component } from '@angular/core';
import { TitleCasePipe } from '@angular/common';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  title = 'Working with Pipe';
  testValue = this.titleCasePipe.transform('firstletter should be upper case.');


  constructor(
    public titleCasePipe: TitleCasePipe
  ){}

}

enter image description here


Solution

  • Add TitleCasePipe in the providers array of the metadata of your component :

    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ],
    providers: [TitleCasePipe]
    })
    

    Stackblitz example