最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to render a hyperlink in a component template in Angular 6? - Stack Overflow

programmeradmin0浏览0评论

I have an Angular 6 application where a ponent is used to display a message on the page. Some of the messages contain hyperlinks embedded in them (in HTML markup). However, when the messages are displayed on the page, they are getting displayed in plain text (hyperlinks are not rendered, but the markup is displayed to the user instead).

You can visit Stackblitz @ for a sample application that I created to explain the issue.

Expected message display:

Click here.

Actual message display:

Click <a href=''>here</a>

I have an Angular 6 application where a ponent is used to display a message on the page. Some of the messages contain hyperlinks embedded in them (in HTML markup). However, when the messages are displayed on the page, they are getting displayed in plain text (hyperlinks are not rendered, but the markup is displayed to the user instead).

You can visit Stackblitz @ https://stackblitz./edit/angular-jj5nms for a sample application that I created to explain the issue.

Expected message display:

Click here.

Actual message display:

Click <a href='http://www.google.'>here</a>

Share Improve this question edited Jul 30, 2018 at 5:24 Vini asked Jul 30, 2018 at 5:15 ViniVini 8,41911 gold badges38 silver badges50 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

If you want to render HTML, you need to bind to the innerHTML property of an element, for example:

<p [innerHTML]="message | async"></p>

Where message is your observable from the service.

Using handlebars to render message is just rendering plain text, binding to innerHTML and using the async will render your html content.

You can use innerHTML

In your ponent:

linkHtml = "Click <a href='http://www.google.'>here</a>"

In your template:

<div [innerHTML]="linkHtml"></div>

You can use like that: in your .ts file :

dummyLinkText: string = "Click <a href='https://www.google.'>here</a>";

and in your .html file:

<div [innerHTML]="dummyLinkText | translate"></div>
发布评论

评论列表(0)

  1. 暂无评论