Angular is a programming framework that uses pipes to place an underscore below a

In order to get the first letter and add an underscore underneath, you can use the following code:

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

@Pipe({
  name: 'underlineFirstLetter'
})
export class underlineFirstLetterPipe implements PipeTransform {
  transform(value: string, args: any[]): string {
    if (value === null) return;
    return value.charAt(0).toUpperCase() + '_' + value.slice(1);
  }
}

If you want to display the underscore under the first letter, you can modify the code as follows:

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

@Pipe({
  name: 'underlineFirstLetter'
})
export class underlineFirstLetterPipe implements PipeTransform {
  transform(value: string, args: any[]): string {
    if (value === null) return;
    return "<u>" + value.charAt(0).toUpperCase() + "</u>" + value.slice(1);
  }
}

If it's still not displaying correctly, you may need to adjust the styling accordingly.

Answer №1

To achieve this effect using CSS only, you can utilize the ::first-letter selector:

p::first-letter {
  text-transform: capitalize;
  text-decoration: underline;
}
<p>underline & adjust case of my initial letter!</p>

Answer №2

HTML Code

<p *ngFor="let name of names" [innerHTML]="name | underlineFirstLetter"></p>

Pipe Implementation

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

@Pipe({
  name: 'underlineFirstLetter'
})
export class UnderlineFirstLetterPipe implements PipeTransform {
  transform(value: string, args: any[]): string {
    if (value === null) return;
    return "<u>" + value.charAt(0).toUpperCase() + "</u>" + value.slice(1);
  }
}

Link to Stackblitz

Update:

Addressing concerns about complexity and correctness raised by @trichetriche, here is a simplified approach to HTML sanitization in Angular

Below you will find the updated sanitized code

import { Pipe, PipeTransform, SecurityContext } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

@Pipe({
  name: 'underlineFirstLetter'
})
export class UnderlineFirstLetterPipe implements PipeTransform {

  constructor(private _sanitizer: DomSanitizer) {}

  transform(value: string, args: any[]): string {
    if (value === null) return;
    return this._sanitizer.sanitize(SecurityContext.HTML, "<u>" + value.charAt(0).toUpperCase() + "</u>" + value.slice(1));
  }
}

The Stackblitz has also been updated with these changes

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular component as well as its corresponding helper service or utility class

I recently started using Angular 5 and I'm working on creating a component with a lot of logic to prepare data for display in HTML. From what I understand, it's recommended to move this logic out of the component to avoid triggering change track ...

Modify the PrimeFaces dialog to be modal using client-side code

One of my primefaces dialogs is set up like this: <p:dialog widgetVar="dlg" width="320" height="220" modal="false" closable="false" showHeader="false" resizable="false" position="right,top"> I am looking to make this dialog modal if a certain eleme ...

What is the best way to include a mat-paginator with mat-cards?

Just starting out with Angular and trying to implement pagination for mat-cards instead of just tables. I have a lot of code and want to display only 8-10 cards per page. How can I achieve this? Below is my HTML and TypeScript code. .html file <div cl ...

Transforming a string into key-value pairs using Typescript

Is there a way to transform the given string into key-value pairs? TotalCount:100,PageSize:10,CurrentPage:1,TotalPages:10,HasNext:true,HasPrevious:false ...

Demonstrating various elements within an application using Vue3

I created two components and attempted to display them in a Vue 3 application. Here is my HTML code: <div id="app"> <image_preview> URL: [[image]] </image_preview> <file_uploader> Counter:[[coun ...

The value chosen by the user is not retained by the Select function

Having some trouble with a simple select issue in Angular that is causing my select options to disappear after clicking on them. Here's my code: var app = angular.module("myApp", []); app.controller("myCtrl", function($scope) { $scope.searchFilt ...

Prevent side menu from automatically hiding when clicking on the heading

My menu is set up with headings and subheadings. When I click on the headings, it expands to display the corresponding subheadings, but it automatically collapses when clicking on the headings. I want it to stay expanded when clicking on the headings. He ...

Tips on formatting the date model field in an Angular 2 form

I have a model with a date field in MySQL format "yyyy-mm-dd hh:ii:ss" When displaying this field in my form, I want to show it in an input with a custom format: "dd/mm/yyyy" <input [(ngModel)]="model.date" name="date" view-format="DD/MM/YYYY" model-f ...

Is there a way for me to invoke a different function that is located external to

I am currently in the process of setting up an event that triggers on any change within the form input class. import React, { useState } from "react"; DealerRegistration extends React.Component { state = { business_type : 'd ...

Preventing Firebase duplicates leads to the error of not being able to read the property 'apps'

Struggling to incorporate Firebase into a TypeScript/NextJS project, I have encountered difficulties. Despite successfully importing and initializing the app: import * as firebase from "firebase/app"; import { collection, getDocs } from "fir ...

Storing dataset characteristics in a JSON file utilizing Vue.js form response

I am currently working on creating a JSON file to store all the answers obtained from a Form. Some of the input fields have an additional dataset attribute (data-tag). When saving the Form, I aim to extract these 'tags' and include them in the JS ...

Oops! Issue: The mat-form-field is missing a MatFormFieldControl when referencing the API guide

I included the MatFormFieldModule in my code like so: import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; ...

Exploring Angular: A Guide to Localisation and JSON

Looking at my JSON database structure below: { "users" : { "-Kowtg5yyK-DTIz91cQ8" : { "language" : "en", "uid" : "kNyDJnktxyakL6owhGd1rruGACb2", "userName" : "admin" } }, "localisation" : { "login" : { "en" : "Log ...

issue with mat-select in a dialog not functionering correctly

In my current project, I am working on implementing a feature that requires the user to select an option from a drop-down menu and confirm their choice by clicking an "OK" button. To achieve this functionality, I am utilizing Angular Material. However, I h ...

What is the best way to transfer a variable from a template to views.py?

Is there a way for me to pass the oobcode value to the postresetusername function in views.py? reset_username.html <script type="text/javascript"> var oobcode; function func(){ oobcode = localStorage.getItem("storageName"); ...

Resend the octet-stream data from an external API

I'm currently facing a challenge with retransmitting data received as octet-stream from an external API in my nodejs application. My previous attempts to convert the API response into a buffer and send it using res.write(buffer(apiResponse.data)) have ...

The YoutubePlayer's OnStateChange event is not triggering as expected

Having trouble with the YouTube player OnStateChange event not firing. I've researched similar posts but none seem to match my unique situation since I'm using a different version (chromeless JavaScript). Here's the HTML: <div id="ytapi ...

jquery unclean data, in need of the jquery ui popup dialog

I have been experimenting with this library to determine if a form is dirty. By default, it triggers the standard browser confirmation asking whether you are certain about navigating away from the page. The jquery dirtyforms website includes a section sugg ...

"Be sure to add a table to the req.body information

Building on the topics of my recent inquiries about fetching data from async functions and adding IDs to checkboxes using loops on Stack Overflow, I am faced with a new challenge. I am attempting to retrieve the checked checkboxes using Node.js and Express ...

Get your hands on the latest version of Excel for Angular

214/5000 I am currently facing an issue in Angular where I am attempting to generate an excel file. Within the file, there is a "Day" column that is meant to display numbers 1 through 31. However, when attempting this, only the last number (31) is being pr ...