I'm pretty new to angular, so I hope this question isn't too basic. I'm trying to run a function in angular, but I seem to have an issue with the scope of my variables. This is what it looks like:
export class Skulder14q1Component {
button1 = document.getElementById('button1');
button2 = document.getElementById('button2');
button3 = document.getElementById('button3');
button4 = document.getElementById('button4');
showallbutton = document.getElementById('showallbutton');
onShowAllOptions() {
this.button1.classList.toggle('hide');
this.button2.classList.toggle('hide');
this.button3.classList.toggle('hide');
this.button4.classList.toggle('hide');
this.showallbutton.classList.add('hide');
}
Now the function is run by the press of a button, but when pressing the button, the console log returns this:
> Skulder14q1Component.html:12 ERROR TypeError: Cannot read property 'classList' of null
at Skulder14q1Component.onShowAllOptions (skulder14q1.component.ts:28)
at Object.eval [as handleEvent] (Skulder14q1Component.html:15)
at handleEvent (core.js:10251)
at callWithDebugContext (core.js:11344)
at Object.debugHandleEvent [as handleEvent] (core.js:11047)
at dispatchEvent (core.js:7710)
at core.js:8154
at HTMLButtonElement.<anonymous> (platform-browser.js:988)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3811)
Changing to this code instead works:
onShowAllOptions() {
const button1 = document.getElementById('button1');
const button2 = document.getElementById('button2');
const button3 = document.getElementById('button3');
const button4 = document.getElementById('button4');
const showallbutton = document.getElementById('showallbutton');
button1.classList.toggle('hide');
button2.classList.toggle('hide');
button3.classList.toggle('hide');
button4.classList.toggle('hide');
showallbutton.classList.add('hide');
}
I feel like I'm missing something basic, and I hope you can help me out. Thanks!