Last active
March 18, 2017 09:27
-
-
Save djabif/0f6dbc7045f95cfad8ed to your computer and use it in GitHub Desktop.
Directive to open external links in Ionic App using inAppBrowser cordova plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which location to insert the code?