hi guys why nav controllers not working on this function , how to fix it ?
let classname = document.getElementsByClassName("product-image");
function myFunction() {
var attribute = this.getAttribute("class");
var product_id= attribute.match(/\b\d*/);
this.navCtrl.push(ProductDetailPage,{id: product_id})
};
for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener('click', myFunction, false);
}
Hm, do you get any errors?
Make sure your imports are in the correct directories (if you have to go back one root …/);
try these:
• Try making the navCtrl public
• Try putting your code inside the constructor
• Try adding your custom page/etc to the app.modules.ts and/or app.componets.ts
Cause it seems like you’re not inside somewhere to be granted access so it’s being thrown “undefined”.
Pretty much everything about this code sample is completely unidiomatic. You should not be directly interacting with the DOM this way in an Angular application - use property and event bindings instead. If you can describe in larger terms what you are really trying to do, perhaps we can help you rewrite it in a more natural way.
That being said, the proximate answer to your question is that you cannot rely on execution context (aka what ‘this’ means) in a function you declare using function, which is why I recommend always using a lambda or arrow function instead.
i told to you i can access to nav controller on other function but i cant access to nav controller on this function
let classname = document.getElementsByClassName("product-image");
function myFunction() {
var attribute = this.getAttribute("class");
var product_id= attribute.match(/\b\d*/);
this.navCtrl.push(ProductDetailPage,{id: product_id})
};
for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener('click', myFunction, false);
}
and i parse message attribute to view( with innerHtml attribute) , i want when user clicked on image go to ProductDetailPage all code is works fine but nav controller not working on DOM