-
-
Save rewonc/e53ad3a9d6ca704d402e to your computer and use it in GitHub Desktop.
//* | |
//* See the JS Fiddle: http://jsfiddle.net/sxcjmoj5/3/ | |
//* | |
//* | |
// make sure ngSanitize module is installed: | |
// https://docs.angularjs.org/api/ngSanitize | |
// | |
// To convert links from plaintext, you must use the linky filter which is included with ngSanitize | |
// https://docs.angularjs.org/api/ngSanitize/filter/linky | |
// | |
// You might need to install inappbrowser if you haven't done that: | |
// https://github.com/apache/cordova-plugin-inappbrowser/blob/master/doc/index.md | |
// | |
// In your app.js | |
var myApp = angular.module('myApp', ['ngSanitize']); | |
myApp.filter('hrefToJS', function ($sce, $sanitize) { | |
return function (text) { | |
var regex = /href="([\S]+)"/g; | |
var newString = $sanitize(text).replace(regex, "onClick=\"window.open('$1', '_blank', 'location=yes')\""); | |
return $sce.trustAsHtml(newString); | |
} | |
}); | |
myApp.controller('MyCtrl', function ($scope) { | |
$scope.html = "This a link: <a href='https://www.google.com'>Google</a> :)"; | |
$scope.plaintext = "This is a link: https://www.google.com :) " | |
}); | |
///Usage in your HTML template | |
/* | |
<div ng-app="myApp"> | |
<div ng-controller="MyCtrl"> | |
<h1>Before</h1> | |
<p ng-bind-html="html"></p> | |
<p ng-bind-html="plaintext"></p> | |
<h1>After</h1> | |
<p ng-bind-html="html | hrefToJS"></p> | |
<p ng-bind-html="plaintext | linky | hrefToJS"></p> | |
</div> | |
</div> | |
*/ |
hello, can any one help me...
am new to angularjs and phone as well as phonegap..
i have built app for android in ionic framework, the link which i would gave it download the page but when i installed in android mobile it will not work.
where i am wrong.
plz suggest me..
great
Perfect!
I love this community.
Thanks. Good works.
Thank you.
It's saved my life.Thank you so much.
Even the "naked" ngSanitize offers something like this https://code.angularjs.org/1.4.12/docs/api/ngSanitize/filter/linky
Once you need something more advanced (hashtags, mentions) take a look at https://github.com/scottcorgan/angular-linkify
Thaks.
perfect
I've got this working on android after building an angular 1.5 app through ionic/cordova build. But on iOS the outgoing link does not open safari. It goes to the app's home page, which I'm assuming is the "otherwise" part of the routes kicking in. I've got inappbrowser installed. Perhaps that's not configured correctly?
Thanks for this! My Ionic1 app doesn't open links in the browser (webview) mode when target=_blank, instead it requires target=_system. I adapted the code slightly and now it works for me in both browser mode and iOS. If you're having the same problem, try this:
var newString = $sanitize(text).replace(regex, "href onClick=\"window.open('$1', '" + (ionic.Platform.isWebView()) ? '_system' : '_blank' + "', 'location=yes');return false;\"");
@niyando: Perfect! Thank you so much :)