如何检测HTML中可点击的链接

huangapple go评论89阅读模式
英文:

How to detect clickable link in HTML

问题

I am getting a response in a list from backend and putting error codes to a table as in picture
如何检测HTML中可点击的链接

This explanation string has a URL in it as you see www.google... I want to make it clickable but I cannot.

When I try to replace the link with regex with following codes (my model object for the table item is response: errorArray.slice(1, errorArray.length));

const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
const errorArray = errorResponse.error.toString()
  .replace(/\[/g, "")
  .replace(/]/g, "")
  .replace(urlRegex, "<a href='$1' target='_blank'></a>")
  .replace(/\.,/g, ".");
this.allUploadedPinsFiles.push({uploadDate: uploaded, fileName: file.name,
          status: status, responseHeader: errorArray[0], response: errorArray.slice(1, errorArray.length)});

It becomes
如何检测HTML中可点击的链接

It is replacing item in the error array but in UI it still shows as a text. How can I make it that it can understand the link in a text and make it clickable?

and here is the html code for that list:

<div *ngIf="upload.response.length > 0">
  <ul class="list-item">
    <li *ngFor="let singleResponce of upload.response">{{singleResponce}}</li>
  </ul>
</div>
英文:

I am getting a response in a list from backend and putting error codes to a table as in picture
如何检测HTML中可点击的链接

This explanation string has a URL in it as you see www.google... I want to make it clickable but I cannot.

When I try to replace the link with regex with following codes (my model object for the table item is response: errorArray.slice(1, errorArray.length));

const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&amp;@#\/%?=~_|!:,.;]*[-A-Z0-9+&amp;@#\/%=~_|])/ig;
        const errorArray = errorResponse.error.toString()
          .replace(/\[/g, &quot;&quot;)
          .replace(/]/g, &quot;&quot;)
          .replace(urlRegex, &quot;&lt;a href=&#39;$1&#39; target=&#39;_blank&#39; &gt;&lt;/a&gt;&quot;)
          .replace(/\.,/g, &quot;.&quot;)
this.allUploadedPinsFiles.push({uploadDate: uploaded, fileName: file.name,
          status: status, responseHeader: errorArray[0], response: errorArray.slice(1, errorArray.length)});

It becomes
如何检测HTML中可点击的链接

It is replacing item in the error array but in UI it still shows as a text. How can I make it that it can understand the link in a text and make it clickable?

and here is the html code for that list:

&lt;div *ngIf=&quot;upload.response.length &gt; 0&quot;&gt;
          &lt;ul class=&quot;list-item&quot;&gt;
            &lt;li *ngFor=&quot;let singleResponce of upload.response&quot;&gt;{{singleResponce}}&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/div&gt;

答案1

得分: 1

将数据进行清理并传递给HTML:

this.sanitizer.bypassSecurityTrustHtml("&lt;a href=&#39;www.google.com&#39; target=&#39;_blank&#39;&gt;My Link&lt;/a&gt;")

尝试这个:

工作链接

英文:

Sanitize the data and pass to the html

this.sanitizer.bypassSecurityTrustHtml(&quot;&lt;a href=&#39;www.google.com&#39; target=&#39;_blank&#39;&gt;My Link&lt;/a&gt;)

Try this

Working link

答案2

得分: 0

使用 innerHtml

<li *ngFor="let singleResponse of upload.response">
    <span [innerHtml]="singleResponse"></span>
</li>
英文:

Use innerHtml

 &lt;li *ngFor=&quot;let singleResponce of upload.response&quot;&gt;
     &lt;span [innerHtml]=&quot;singleResponce&quot;
 &lt;/li&gt;

huangapple
  • 本文由 发表于 2020年1月3日 17:55:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定