Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 474.2k

Creating a function inside a class in Angular 13

Mar 7 2023 5:14 AM

Hi Team

 

I have this code that creates a switch toggle button on and off, but when i try to create a function to call this property within a class. Its complaining its expecting a constructor or method. How do i fix this error "unexpected token"...?

 

According from the online IDE i am expected to work only on this page this is no Angular CLI project from local IDE.

`// code in Angular js 13
import { Component} from '@angular/core';

@Component({
selector: 'toggle-button',
template: `
<div class="toggle-button">
    <button (click)="toggleState()">{{ yourVariableName === true ? "ON" : "OFF" }} />
</div>
`})

export class ToggleButton {
    buttonState: boolean = false;

    function toggleState() { // unexected token has an error
        this.buttonState != this.buttonState;
    }
}`

 


Answers (3)