Events — Angular Technical Interview Questions & Answers
Events
Events
- Events: (click), (input), (submit), (keyup.enter). The handler lives on the class. $event is the browser event when you need the value.
- Board: <form (ngSubmit)="save($event)">. Call $event.preventDefault() or use ngSubmit so the page doesn’t reload. Trap: (click) on a submit button without preventDefault.
Technical Interview Tip: (click) vs ngSubmit. preventDefault.
Code Examples for Events
Events — working example
import { Component } from "@angular/core";
@Component({
selector: "app-root",
template: `
<h1>{{ title }}</h1>
<button (click)="toggle()">Toggle</button>
<p *ngIf="open">Panel open</p>
`,
})
export class AppComponent {
title = "Angular practice";
open = true;
toggle() {
this.open = !this.open;
}
}Events: toggle open. Say whether the panel is on screen after one click.
How to Explain Events to an Interviewer
How do you handle a click in Angular?
(click)="save()" Method on the class