Skip to content

Instantly share code, notes, and snippets.

@erayarslan
Created January 5, 2017 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erayarslan/2b73a88cff36326f8db969d67bb4e8d5 to your computer and use it in GitHub Desktop.
Save erayarslan/2b73a88cff36326f8db969d67bb4e8d5 to your computer and use it in GitHub Desktop.
angular click to route trigger for links
/**
* @author Eray Arslan
* @description backbone click to route trick implementation
*/
(function () {
'use strict';
angular.module('app')
.directive('glClick', glClick);
/** @ngInject */
function glClick($document, $location, $rootScope) {
return {
link: function (scope) {
function onClick(e) {
var href = {
prop: angular.element(e.target).prop('href'),
attr: angular.element(e.target).attr('href')
};
var root = location.protocol + '//' + location.host;
root += "/";
if (href.prop &&
href.prop.slice(0, root.length) === root &&
href.attr !== "") {
console.log("[glClick] Routing to", href.attr);
e.preventDefault();
$location.url(href.attr);
$rootScope.$apply();
}
}
function cleanUp() {
angular.element($document).off('click', onClick);
}
angular.element($document).on('click', onClick);
scope.$on('$destroy', cleanUp);
}
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment