Directive to render URLs as links?

277 views
Skip to first unread message

Pertti Kellomäki

unread,
Apr 22, 2014, 9:42:58 AM4/22/14
to ang...@googlegroups.com
Does anyone happen to have a directive handy that would render a string as plain text, but turn URLs into links?
--
Pertti

Gabriel Aszalos

unread,
Apr 23, 2014, 5:03:50 AM4/23/14
to ang...@googlegroups.com
Hi Pertti,

I'm not quite sure what your particular use-case is but you might want to look into these services: $sanitize (https://docs.angularjs.org/api/ngSanitize/service/$sanitize) and $sce (https://docs.angularjs.org/api/ng/service/$sce)

Gabriel

Gabriel Aszalos

unread,
Apr 23, 2014, 5:04:33 AM4/23/14
to ang...@googlegroups.com
P.S. You could also use RegEx to do that.


On Tuesday, 22 April 2014 14:42:58 UTC+1, Pertti Kellomäki wrote:

Pertti Kellomäki

unread,
Apr 23, 2014, 3:22:37 PM4/23/14
to ang...@googlegroups.com
Hi Gabriel,

I looked around a bit and found <https://github.com/gregjacobs/Autolinker.js> which can create links from URLs, email addresses and Twitter handles. I wrapped it in a directive like this:

    .directive('autolinker', function () {
return {
   restrict: 'E',
   scope: {
text: '='
   },
   link: function (scope, element, attrs) {
scope.$watch("text", function (new_value) {
   if (new_value != undefined) {
element.html(Autolinker.link(new_value, {email: false, twitter: false}));
   }
})
   }
}
    })

Pertti


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Pertti Kellomäki, Kesanto Enterprises
Your training in the cloud: <http://www.trainingcommons.com>

Gabriel Aszalos

unread,
Apr 23, 2014, 4:22:52 PM4/23/14
to ang...@googlegroups.com
Nice :) Glad it worked out
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/DRypnFRQ4pg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

Ryan Alberts

unread,
Jun 29, 2014, 11:38:38 PM6/29/14
to ang...@googlegroups.com
Thanks!  I am going to try this out as I was looking for the same thing.
Reply all
Reply to author
Forward
0 new messages