At times we come across few cases where we need to differentiate the links amongst the plain text. Angular provide us with a very useful filter named : “linky” which help us to do so.
With the help of linky filter we can detect links from text and show them differently . Linky takes text and produces HTML by wrapping all URLs into anchor tags. It supports email address , http, https, and mailto.
For this you would require the ngSanitize module in angular app.
In HTML binding
<p ng-bind-html=”myText | linky”> </p>
You can also define the target for high-lighted links like this-
<p ng-bind-html=”myText | linky :’_blank'”>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>Lets try linky filter</title> <script type="application/javascript" src="angular.js"></script> <script src="LinkyCtrl.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular-sanitize.min.js"></script></head><body><div ng-app="myApp" ng-controller="myController"> <div> <p ng-bind-html="myText | linky :'_blank'"></p> <textarea ng-model="myText" style="width: 420px; height: 120px"></textarea> </div></div></body></html> |
In our angular controller,
1
2
3
4
| var myApp = angular.module('myApp', ['ngSanitize']) .controller('myController', ['$scope', function ($scope) { $scope.myText = "Some default text is here http://www.intelligrape.com/";}]); |
So in this way we can quickly highlight the links in angular.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.