Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

629
Vistas
Angular button (onClick) event not working

I have a button that should call a function that logs the user out. For some reason, the event is not working at all. Here is the code: TS File:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { AuthService } from 'src/app/services/auth.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.sass']
})
export class LoginComponent implements OnInit {
  form: FormGroup;

  constructor(private formBuilder: FormBuilder, private auth: AuthService) { }

  ngOnInit(): void {
    this.form = this.formBuilder.group({
      email: '',
      password: ''
    })
  }

  submit(){
    this.auth.tryLogin(this.form.getRawValue())
    .subscribe(res => {
      console.log(res);
    })
  }

  public logout(){
    console.log("logout")
    this.auth.tryLogout()
    .subscribe(res => {
      console.log(res);
    })
  }

  // public testJWT(){
  //   alert("asd")
  //   this.auth.testAuth()
  //   .subscribe(res => {
  //     console.log(res)
  //   })
  // }
}

And the HTML:

<form [formGroup]="form" (submit)="submit()">
    <h1>Anmelden</h1>
    <input formControlName="email" type="email" placeholder="Email Addresse" required> 
    <br>
    <input formControlName="password" type="password" placeholder="Passwort" required>
    <br>
    <button type="submit">Anmelden</button>
</form>

<button (onClick)='logout()'>log out</button>

I have no idea what I am doing wrong, I tried using this.logout(), I tried type="button" in the html, but nothing worked for me.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It should be click and not onClick.

<button (click)='logout()'>log out</button>

Syntax reference:

syntax

You can refer to the official wiki https://angular.io/guide/event-binding for more info

over 4 years ago · Santiago Trujillo Denunciar

0

The event is "click". you are using onClick which is incorrect. Please change onclick to click and check.

<button (click)='logout()'>log out </button>

Reference: https://angular.io/guide/user-input#binding-to-user-input-events

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda