We have an SPA developed in Angular and I want to fire my analytics code when the router changes, so I want to be able to get the app name or the module name. Below is the code I am trying to use but I am getting error as "ReferenceError: testApp is not defined(…)".
function getAllElementsWithAttribute(attribute)
{
var matchingElements = [];
var allElements = document.getElementsByTagName('*');
for (var i = 0, n = allElements.length; i < n; i++)
{
if (allElements[i].getAttribute(attribute) !== null)
{
// Element exists with attribute. Add to array.
matchingElements.push(allElements[i].getAttribute(attribute));
}
}
return matchingElements[0];
}
if(window.angular != undefined || window.angular != null){
console.log("Angular lOADED");
//var modName = angular.modules[0];
var modName = getAllElementsWithAttribute("ng-app");
try{
var myMod = typeof(eval(modName));
console.log(myMod);
}catch(e){
console.log(e);
}
myMod.run(function($rootScope, $location) {
console.log($routeScope)
$rootScope.$on('$routeChangeSuccess', function () {
if(true){
loadTag($location);
}
});
});
function loadTag(location){
console.log(location);
console.log("Tag Loaded");
}
}