2

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

Shlok Nangia
  • 2,355
  • 3
  • 16
  • 24
Lei De Bug
  • 23
  • 1
  • 4

4 Answers4

6

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

Gérôme Grignon
  • 3,859
  • 1
  • 6
  • 18
2

You can just create an object, else, add in providers as @Gérôms say

  title = 'Working with Pipe';
  titleCasePipe=new TitleCasePipe()
  testValue = this.titleCasePipe.transform('firstletter should be upper case.');

  constructor(){
  }
Eliseo
  • 50,109
  • 4
  • 29
  • 67
0

Put your code inside constructor like this,

 testValue: string;


 constructor(
    public titleCasePipe: TitleCasePipe
 ){
    this.testValue = this.titleCasePipe.transform('firstletter should be upper case.');
 }
Indrajeet
  • 491
  • 3
  • 10
  • the transform function returns the transformed parameter. So it will be useless to call the function if you don't initialize a local variable with it (and you don't need to put it inside the constructor as well) – Gérôme Grignon Apr 14 '20 at 11:34
  • Sorry, I have updated the code, anyways Why you need to have that code inside constructor or ngOnInit() is because is by that time TitleCasePipe is available to use – Indrajeet Apr 14 '20 at 11:36
  • can you share your stackblitz link? – Indrajeet Apr 14 '20 at 11:37
  • the proof you don't need to put in constructor or ngOnInit : https://stackblitz.com/edit/angular-i5bfaj?file=src%2Fapp%2Fapp.component.ts – Gérôme Grignon Apr 14 '20 at 11:39
0

Without Pipe


Here is your Simple Solution with TitleCase

you can use directly like this you don't have to put extra code angular already give's this features

HTML

<p>
  {{name | titlecase}} <!-- this is titlecase by default -->
</p>

TS

name = 'Working with Pipe';

With Pipe

you can check in input field also like this

HTML

<input type="text" id="firstName" name="name" 
[ngModel]="name | titleCase" 
(ngModelChange)="name=$event" 
#firstName="ngModel">

TS

import {Pipe, PipeTransform} from '@angular/core';

@Pipe({name: 'titleCase'})
export class TitleCasePipe implements PipeTransform {
    public transform(input:string): string{
        console.log(input);
        if (!input) {
            return '';
        } else {
            return input.replace(/\b\w/g, first => first.toLocaleUpperCase()) 
        }
    } 
}

and don't forgot to add in declaration like this

declarations: [ TitleCasePipe ],

Here is my stackBlitz you can check here

Kiran Mistry
  • 2,614
  • 3
  • 12
  • 28