Created
January 5, 2017 08:45
-
-
Save erayarslan/2b73a88cff36326f8db969d67bb4e8d5 to your computer and use it in GitHub Desktop.
angular click to route trigger for links
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
/** | |
* @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