Skip to content

Instantly share code, notes, and snippets.

@djabif
Last active March 18, 2017 09:27
Show Gist options
  • Save djabif/0f6dbc7045f95cfad8ed to your computer and use it in GitHub Desktop.
Save djabif/0f6dbc7045f95cfad8ed to your computer and use it in GitHub Desktop.
Directive to open external links in Ionic App using inAppBrowser cordova plugin
//Use this directive to open external links using inAppBrowser cordova plugin
.directive('dynamicAnchorFix', function($ionicGesture, $timeout, $cordovaInAppBrowser) {
return {
scope: {},
link: function(scope, element, attrs) {
$timeout(function(){
var anchors = element.find('a');
if(anchors.length > 0)
{
angular.forEach(anchors, function(a) {
var anchor = angular.element(a);
anchor.bind('click', function (event) {
event.preventDefault();
event.stopPropagation();
var href = event.currentTarget.href;
var options = {};
//inAppBrowser see documentation here: http://ngcordova.com/docs/plugins/inAppBrowser/
$cordovaInAppBrowser.open(href, '_blank', options)
.then(function(e) {
// success
})
.catch(function(e) {
// error
});
});
});
}
}, 10);
},
restrict: 'A',
replace: false,
transclude: false
};
});
@uab234
Copy link

uab234 commented Dec 5, 2016

Which location to insert the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment