Let's say template receives string from component in one way binding, interpolation:
<span>{{list.members}}</span>
And it renders like
5 members
How can I edit string on HTML without changing anything on component so I can get bold number 5 and "members" left untouched:
5 members
Here's a pipe solution:
strong-number.pipe.ts:
@Pipe({name: 'strongifyNumber'})
export class StrongifyNumber implements PipeTransform {
transform(value: string): string {
let values: string[] = value.split(' ')
if(values.length == 2){
return ' <b>'+values[0]+'</b> ' + values[1];
}
return value;
}
}
app.module.ts:
import { StrongifyNumber } from './strong-number.pipe';
@NgModule({
declarations: [
AppComponent, StrongifyNumber
],
imports: [
BrowserModule,
FormsModule
],
providers: [StrongifyNumber],
bootstrap: [AppComponent]
})
template:
<span [innerHTML]="list.members | strongifyNumber"></span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With