7

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.

2 Answers 2

18

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I totally missed that when I read the docs
With the latest version of Material, it has added pointer-events rule and sets it to None. So we have to override, to allow the click events. In html like "class=allow-pointer-events" and inside style sheet .allow-pointer-events { pointer-events: all; }
3

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

1 Comment

thank you, I totally missed that. I looked at the docs but apparenlty I didnt read the code properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.