I am trying to call one of the function in from .js
file in angular whenever a route change happen.
This is my code in the javascript file and I need to call need to call the appData
function in angular.
The console.logs
for console.log(parsedJsonResponse);
and console.log(appData)
works fine and I am getting the JSON response.
window.dataLayer = (function () {
var dataCall = function () {
return new Promise((resolve, reject) => {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 200) {
resolve(this.responseText);
} else {
console.log(this.status + '.....' + this.statusText)
}
}
};
xhttp.open("GET", "http://localhost:4200/assets/sample.json", true);
xhttp.send();
})
}
dataCall().then((CallResponse) => {
getData(CallResponse)
});
window.addEventListener("load", function (event) {
appData();
})
var getData = function (cData) {
jsonResponse = cData.replace(/'/g, '"');
parsedJsonResponse = JSON.parse(this.jsonResponse);
var appData = parsedJsonResponse['content']['copy$$' + applicationContent];
console.log(parsedJsonResponse);
}
I am calling appData
function in
var appData = function (applicationContent) {
var appData = parsedJsonResponse['content']['copy$$' + applicationContent];
console.log(appData)
}
return {
appData: appData,
dataCall: dataCall
}
}())
This is my code in angular calling the function.
When calling this function in angular I'm getting ReferenceError: parsedJsonResponse is not defined
.
constructor(private router:Router) {}
ngOnInit(){
this.router.events
.filter(event => event instanceof NavigationStart)
.subscribe((event: NavigationStart) => {
dataLayer.appData();
})
};
What is going wrong? Thanks.