Show a hyperlink within dynamically retrieved HTML content

As I generate a list of strings dynamically, I am displaying it in Angular using ngFor. However, some of the strings contain hyperlinks which are not being distinguished properly when displayed - they appear as normal text. I want to differentiate these hyperlinks by underlining them.
For example: Check out https://support.google.com/accounts/answer/abc?hl=en# for instructions on 'Creating a Google Account' using an email.

Answer №1

Understanding correctly, it's quite straightforward and can be achieved by following this method:

<a href="{{yourlinkVariable}}">{{Text Variable}}</a>

Answer №2

creating a custom pipe that generates clickable links from URLs within an [innerHtml]

@Pipe({name: 'linkPipe'})
export class LinkPipe implements PipeTransform {
  constructor(private _domSanitizer: DomSanitizer){}
  transform(value: string): any {
    if (value.indexOf("http")>=0)
    {
      //extract the URL
      const link=value.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+~#?&//=]*)?(\[.*\])?/)
      if (link) 
      {
        const valueSplit=link[0].split('[') 
        value=value.replace(link[0],
            "<a href='"+valueSplit[0]+"'>"+
              (valueSplit[1]?valueSplit[1].slice(0,-1):valueSplit[0])+
            "</a>")
      }
    }
    return this._domSanitizer.bypassSecurityTrustHtml(value)
  }
}

example of usage:

<p [innerHTML]="'see the http://www.google.com[Google page] for more info'|linkPipe "></p>
<p [innerHTML]="'http://www.google.com'|linkPipe"></p>

view on stackblitz

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

Converting Selenium Webdriver Xpath value to CSS: A Step-by-Step

We are looking to transform the lengthy HTML Xpath values in the existing code into a shorter CSS value: driver.findElement(By.cssSelector(".form button")).click(); ...

Is it possible to use JavaScript to extract data associated with a specific h2 class on a webpage and then present that information?

Just curious if it's feasible, not looking for actual code. For instance, say I have an h2 class called "stock-number" with a value of #NC123456. Is it possible for JavaScript to access each instance of that class, retrieve data from a different datab ...

Personalized Navigation Bar Toggle (transitioning from dropdown to sliding from the left)

On larger screens, I would like the navigation/navbar to be positioned at the top. However, on mobile view or when collapsed, I want the toggle button to reveal the navigation menu by sliding from the left side, similar to this example: (source by David B ...

What is the method for making text uppercase within the Heading feature of Grommet?

Currently implementing grommet in my React project and exploring how to capitalize text within a few <Heading /> components. ...

Hello, I am looking for a way to maintain a user's active session when transitioning between different Angular applications without constantly prompting them to login. Can you help me

After logging in with my credentials, the session starts. However, if the user does not have an administrator role, I need to redirect them to another application. The challenge is maintaining the active session without requiring the user to log in again ...

I'm attempting to set up three columns that fade in responsively. Is there a way to make them appear side by side instead of stacking on top of each other?

Is there a way to display the three columns horizontally instead of vertically? I also want them to adjust responsively and fade smoothly. Here is my fiddle link: https://jsfiddle.net/Spleendrivel/dswufb78/3/ <!DOCTYPE html> <html> <head ...

Applying JavaScript to HTML - Implementing a function to only display an input statement when the "Yes" option is selected, otherwise keeping it hidden

I am currently delving into the world of JavaScript, aiming to comprehend its intricacies. With limited experience in this language, I have managed to successfully hide and unhide radio buttons based on user input. My next challenge lies in achieving a si ...

It's incredibly frustrating when the Facebook like button is nowhere to be found

Currently, I am in the process of developing a website for a local bakery and have decided to incorporate a Facebook like button on the homepage. After visiting developers.facebook, I proceeded to copy and paste the code provided. It appears that there use ...

When I hover over div 1, I am attempting to conceal it and reveal div 2 in its place

I need help with a CSS issue involving hiding one div on mouse hover and showing another instead. I attempted to achieve this using pure CSS, but both divs end up hidden. Would jQuery be necessary for this task? Below is the CSS/HTML code I wrote: .r ...

How can I make sure the page scrolls to the top when navigating from one component to another in Angular 4 (now 5)?

Recently, I've encountered a problem with routing in my Angular 5 project. My CLI version is 1.5.2. The issue arises when transitioning from one route to another. If I scroll down on the page and then navigate back to the previous route, the page rem ...

How to Customize Background Colors for Blogspot Posts?

Recently, I started my own blog using Google Blogger and I've run into a minor issue. Despite applying a background image for the entire blog, all of my posts have a white background instead. I haven't used any background-color in the HTML code o ...

Tips on avoiding the collapse of the right div when positioned under a left float in a full page layout

Trying out a full page layout for the first time, with a left div serving as a block style nav. #admin_nav { width: 230px; height: 100%; background-color: #f9f9f9; border-right: 1px solid #ddd; float: left; } The content is on the rig ...

Trigger an alert when a button is clicked and redirect the user to an newly opened tab

I recently created a button with a link that opens in a new tab. I also implemented some JavaScript to display an alert. Everything is working as expected, but after the user clicks "OK" on the alert, they remain on the same page. I would like to automati ...

A design featuring two columns and a fixed footer

I'm struggling to make my 2-column layout display properly with a footer that sticks to the bottom of the page. Despite following all the usual advice, my columns keep expanding beyond their container div, which causes the footer to get pushed off the ...

The website does not display properly on larger screens, while showing a scrollbar on smaller screens

I'm encountering an issue with my website where I can't seem to find a solution no matter what I try. My goal is to ensure that my website looks consistent across all computer screen sizes. However, when viewed on larger screens, there's a ...

The grid is out of alignment, causing elements to jump to the next row

After taking an online course at TreeHouse, I used the provided HTML & CSS code and made some adjustments to create a layout with 10 columns instead of the original 12 within a 1000px container. The columns were converted to percentages to fit my needs. I ...

What are the best ways to position elements within a material-ui App Bar?

My goal is to align the items in my navbar similar to this layout (replacing 'Buy on App Store' with a Search Box): https://i.sstatic.net/4NOhU.png I'm drawing inspiration from this material-ui component: https://material-ui.com/components ...

what is the process for making a box similar to this using css3

I have a specific vision for the design: Please follow this link to view the readmore image. I would like it styled with CSS3. ...

The method is unable to assign a value to the undefined property 'var'

I am encountering a simple issue that is giving me an error message TypeError: Cannot set property 'question' of undefined at setQuestion I am struggling to identify the root cause of this problem. As I am new to AngularJS, I wonder if ...

Issues with expected identifiers and type mismatch errors encountered when defining a TypeScript class

As someone who is still relatively new to coding, I am facing a challenge while trying to create a TypeScript class within an Angular project that is based on a complex JSON file. The issue arises from the way the properties were defined in the JSON using ...