SPA가 Angular로 개발되었으며 라우터 변경시 분석 코드가 실행되기 때문에 앱 이름이나 모듈 이름을 가져오고 싶습니다. 아래 코드를 사용하려고하지만 "ReferenceError : testApp 정의되지 않았습니다 (...)"오류가 발생합니다.각도, 앱 또는 모듈 이름을 동적으로 가져옵니다.
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");
}
}
요구에 따라로드하려면 require.js를 사용할 수 있습니다. 모듈에 대한 특별한 문서를보세요. https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md –