Skip to content

Commit fefcb01

Browse files
committed
Parse URL
1 parent 515e369 commit fefcb01

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

simple-todos-angular.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ <h3>
2929
<md-list>
3030
<md-list-item ng-repeat="task in tasks">
3131
<md-checkbox ng-checked="task.checked" ng-click="setChecked(task)"></md-checkbox>
32-
<p>{{task.text}}</p>
32+
<p parse-url="props" ng-model="task.text">{{task.text}}</p>
3333
<md-switch
3434
ng-if="task.owner === $root.currentUser._id"
3535
ng-model="task.private"

simple-todos-angular.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ if (Meteor.isClient) {
6969
});
7070

7171
});
72+
73+
module.directive('parseUrl', function () {
74+
var urlPattern = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/gi;
75+
return {
76+
restrict: 'A',
77+
require: 'ngModel',
78+
replace: true,
79+
scope: {
80+
props: '=parseUrl',
81+
ngModel: '=ngModel'
82+
},
83+
link: function compile(scope, element, attrs, controller) {
84+
scope.$watch('ngModel', function (value) {
85+
var html = value.replace(urlPattern, '<a target="_blank" href="$&">$&</a>');
86+
element.html(html);
87+
});
88+
}
89+
};
90+
});
7291
}
7392

7493
Meteor.methods({

0 commit comments

Comments
 (0)