[英]AngularJS auto convert link in text variable to clickable link
I have the below HTML, to display all article
in listAllArticle
. 我有以下HTML,以显示listAllArticle
所有article
。 The article description
field can sometimes contains a link. 文章description
字段有时可以包含链接。 Now I want to convert the link to an href
that is clickable. 现在我想将链接转换为可点击的href
。 An example of the description I love wwww.google.com
, here the url should be converted to a <a>
element with href
to the link I love wwww.google.com
的描述I love wwww.google.com
,这里的url应转换为带有href
链接的<a>
元素
<table class="table table-bordered table-hover">
<tr>
<th>Title</th>
<th>Description</th>
<th>Content</th>
</tr>
<tr ng-repeat="article in listAllArticles" ng-class-even="'info'" ng-class-odd="'success'">
<td col-2>{{ article.title }}</td>
<td col-2><div ng-bind-html="article[description ] | linky"></div></td>
<td col-4>{{ article.content }}</td>
</tr>
Attempt : I have followed this guide to use AngularJS linky . 尝试 :我已按照本指南使用AngularJS linky 。 In the above, I have put <div ng-bind-html="article[url] | linky"></div>
to be converted. 在上面,我已经把<div ng-bind-html="article[url] | linky"></div>
转换成了。
Also, in my js file, I have also included ngSantitize
as below: 此外,在我的js文件中,我还包括ngSantitize
如下:
var app = angular.module('app', ['ngSanitize']);
Yet the result is not displayed (it shows empty). 但结果未显示(显示为空)。 Could you help? 你能帮忙吗?
In your Html just change article[description] to article.description . 在您的Html中只需将文章[description]更改为article.description 。 And it will work the way you are expecting. 它会按照你期望的方式工作。
Final HTML Code will look like: 最终的HTML代码如下所示:
<table class="table table-bordered table-hover">
<tr>
<th>Title</th>
<th>Description</th>
<th>Content</th>
</tr>
<tr ng-repeat="article in listAllArticles" ng-class-even="'info'" ng-class-odd="'success'">
<td col-2>{{ article.title }}</td>
<td col-2><div ng-bind-html="article.description | linky"></div></td>
<td col-4>{{ article.content }}</td>
</tr>
</table>
This is the test data used: 这是使用的测试数据:
$scope.listAllArticles = [
{
title:"",
description:"I love www.google.com",
content:""
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.