It's a good example to use Angular's pipe:
Create a pipe: mask.pipe.ts:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'mask' })
export class MaskPipe implements PipeTransform {
transform(phrase: string) {
let toBeReplaced = phrase.slice(0, 7);
return phrase.replace(toBeReplaced, "xxx-xxx");
}
}
Put the pipe in declarations of your module:
import { MaskPipe } from "./mask.pipe";
@NgModule({
declarations: [ MaskPipe ]
// ...
})
Use the pipe in your template:
// Component's class:
export class AppComponent {
number: string = "123-123-1234";
}
// Component's template:
<h1> {{ number | mask }}</h1>
The value of number doesn't change, only the displayed value change