[英]How to render a hyperlink in a component template in Angular 6?
I have an Angular 6 application where a component is used to display a message on the page. 我有一个Angular 6应用程序,其中的组件用于在页面上显示消息。 Some of the messages contain hyperlinks embedded in them (in HTML markup).
一些消息包含嵌入其中的超链接(以HTML标记)。 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.com/edit/angular-jj5nms for a sample application that I created to explain the issue. 您可以访问Stackblitz @ https://stackblitz.com/edit/angular-jj5nms,获取我创建的用于解释该问题的示例应用程序。
Expected message display: 预期的消息显示:
Actual message display: 实际消息显示:
Click <a href=' http://www.google.com '>here</a>
点击<a href=' http://www.google.com'>此处</a>
If you want to render HTML a then you need to bind to the innerHTML
property of an element, for example: 如果要呈现HTML,则需要绑定到元素的
innerHTML
属性,例如:
<p [innerHTML]=“message | async”></p>
Where message
is your observable from the service. 您可以从服务中看到
message
的地方。
Using handlebars to render message
is just rendering plain text, binding to innerHTML
and using the async
will render your html content 使用把手来呈现
message
只是呈现纯文本,绑定到innerHTML
并使用async
将呈现您的html内容
You can use innerHTML
您可以使用
innerHTML
In your component: 在您的组件中:
linkHtml = "Click <a href='http://www.google.com'>here</a>"
In your template: 在您的模板中:
<div [innerHTML]="linkHtml"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.