Bird
0
0

You want to create a child component that emits an event with a number when a button is clicked. Which code snippet correctly implements this?

hard📝 component behavior Q8 of 15
Angular - Component Interaction
You want to create a child component that emits an event with a number when a button is clicked. Which code snippet correctly implements this?
Aimport { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: '<button (click)=\"sendNumber()\">Click</button>' }) export class ChildComponent { @Output() numberClicked = new EventEmitter<number>(); sendNumber() { this.numberClicked.emit(42); } }
Bimport { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: '<button (click)=\"sendNumber()\">Click</button>' }) export class ChildComponent { @Output numberClicked = new EventEmitter<number>(); sendNumber() { this.numberClicked.emit('42'); } }
Cimport { Component, Input, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: '<button (click)=\"sendNumber()\">Click</button>' }) export class ChildComponent { @Input() numberClicked = new EventEmitter<number>(); sendNumber() { this.numberClicked.emit(42); } }
Dimport { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: '<button (click)=\"sendNumber()\">Click</button>' }) export class ChildComponent { @Output() numberClicked = new EventEmitter<string>(); sendNumber() { this.numberClicked.emit(42); } }
Step-by-Step Solution
Solution:
  1. Step 1: Check @Output decorator and initialization

    import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: '' }) export class ChildComponent { @Output() numberClicked = new EventEmitter(); sendNumber() { this.numberClicked.emit(42); } } correctly uses @Output() with parentheses and initializes numberClicked as EventEmitter.
  2. Step 2: Verify emit argument type

    import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: '' }) export class ChildComponent { @Output() numberClicked = new EventEmitter(); sendNumber() { this.numberClicked.emit(42); } } emits the number 42, matching the EventEmitter type.
  3. Final Answer:

    Option A correctly implements the child component emitting a number event. -> Option A
  4. Quick Check:

    Match EventEmitter type and emitted value [OK]
Quick Trick: Match EventEmitter type with emitted value type [OK]
Common Mistakes:
  • Omitting parentheses in @Output
  • Mismatching emitted value type
  • Using @Input instead of @Output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes