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

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

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));

  1. const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  2. const errorArray = errorResponse.error.toString()
  3. .replace(/\[/g, "")
  4. .replace(/]/g, "")
  5. .replace(urlRegex, "<a href='$1' target='_blank'></a>")
  6. .replace(/\.,/g, ".");
  7. this.allUploadedPinsFiles.push({uploadDate: uploaded, fileName: file.name,
  8. 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:

  1. <div *ngIf="upload.response.length > 0">
  2. <ul class="list-item">
  3. <li *ngFor="let singleResponce of upload.response">{{singleResponce}}</li>
  4. </ul>
  5. </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));

  1. const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&amp;@#\/%?=~_|!:,.;]*[-A-Z0-9+&amp;@#\/%=~_|])/ig;
  2. const errorArray = errorResponse.error.toString()
  3. .replace(/\[/g, &quot;&quot;)
  4. .replace(/]/g, &quot;&quot;)
  5. .replace(urlRegex, &quot;&lt;a href=&#39;$1&#39; target=&#39;_blank&#39; &gt;&lt;/a&gt;&quot;)
  6. .replace(/\.,/g, &quot;.&quot;)
  7. this.allUploadedPinsFiles.push({uploadDate: uploaded, fileName: file.name,
  8. 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:

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

答案1

得分: 1

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

  1. 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

  1. 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

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

Use innerHtml

  1. &lt;li *ngFor=&quot;let singleResponce of upload.response&quot;&gt;
  2. &lt;span [innerHtml]=&quot;singleResponce&quot;
  3. &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:

确定