如何在angularjs中的字符串中创建链接标记?

时间:2016-11-22 07:10:49

标签: javascript html angularjs json string

我有包含链接的字符串,我想让链接可点击。在角度我怎么会这样做。

以下是我所拥有的json数据的字符串示例"我将于9月27日在@BillieFaiers主持#AFF Catwalk Show @Bullring。点击此处了解更多信息> http://www.clickhereformoreinfo.com"

基本上我想要标签符号和链接都是标签。请帮助这几个小时挣扎。

2 个答案:

答案 0 :(得分:2)

试试这个:

查看

<div ng-repeat="text in texts">
    <p ng-bind-html="text | parseUrl"></p>
</div>

指令

angular.module('filters', [])
    .filter('parseUrl', function() {
        var urls = /(\b(https?|ftp):\/\/[A-Z0-9+&@#\/%?=~_|!:,.;-]*[-A-Z0-9+&@#\/%=~_|])/gim
        var emails = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim

        return function(text) {
            if(text.match(urls)) {
                text = text.replace(urls, "<a href=\"$1\" target=\"_blank\">$1</a>")
            }
            if(text.match(emails)) {
                text = text.replace(emails, "<a href=\"mailto:$1\">$1</a>")
            }

            return text
        }
    });

https://plnkr.co/edit/k4vL5yeHHyLqYu7el7b5?p=preview

答案 1 :(得分:0)

试试这个:

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl',function($scope) {
    var str = "I’m hosting #AFF Catwalk Show @Bullring on 27th Sept with @BillieFaiers. Find out more here > http://www.clickhereformoreinfo.com";
    var subStr = str.split(">");
    $scope.name = subStr[0];
    $scope.link = subStr[1];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  {{name}}<a href="link">{{link}}</a>
</div>