Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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