Angular Components and Directives Quiz Quiz

Explore and reinforce your understanding of Angular components, structural and attribute directives, lifecycle hooks, and component communication. Ideal for intermediate learners looking to strengthen their skills in building scalable Angular applications.

  1. Component Selector Usage

    In Angular, which property of the @Component decorator defines how a component can be invoked in a template, for example as u003Capp-headeru003E?

    1. selector
    2. providers
    3. templateUrl
    4. moduleId

    Explanation: The selector property specifies the custom HTML tag that will be used to insert this component in templates. The templateUrl property points to the HTML file for the component's view, not how it's called. Providers is for dependency injection, and moduleId is rarely used and unrelated to templates. Thus, selector is the correct answer.

  2. Structural Directive Identification

    Which of the following is an example of a built-in structural directive in Angular that can add or remove elements based on a condition?

    1. (ngSubmit)
    2. *ngIf
    3. (ngIf)
    4. [ngClass]

    Explanation: *ngIf is a structural directive that dynamically adds or removes elements from the DOM depending on the provided condition. [ngClass] is an attribute directive used to change classes, not structure. (ngSubmit) is an event binding, and (ngIf) is a typo, as Angular requires the asterisk notation for structural directives.

  3. Lifecycle Hook Purpose

    Which lifecycle hook is implemented in a component to respond after Angular sets or updates input properties, such as receiving new @Input values?

    1. ngOnDestroy
    2. ngDoCheck
    3. ngOnChanges
    4. ngOnInit

    Explanation: ngOnChanges is called whenever data-bound input properties change, making it suitable for handling @Input value updates. ngOnInit runs once after the component initializes, not on every update. ngOnDestroy is for cleanup, and ngDoCheck is a custom change detection method, but does not capture input property changes automatically.

  4. Attribute Directive Role

    What is the primary role of an attribute directive like [ngStyle] in Angular templates?

    1. To handle child component outputs
    2. To create new template structures based on a condition
    3. To register new services for dependency injection
    4. To dynamically change the appearance or behavior of an element

    Explanation: Attribute directives modify the appearance or behavior of existing elements by changing properties, styles, or classes. They do not alter the DOM structure or create new elements, so creating structures is incorrect. Service registration is unrelated, and handling outputs pertains to event binding, not attribute directives.

  5. Parent-Child Communication

    When a child component needs to emit an event to its parent, which Angular mechanism is typically used for this purpose?

    1. @Output with EventEmitter
    2. @Input binding
    3. ngModel two-way binding
    4. ViewChild query

    Explanation: @Output with EventEmitter allows child components to raise custom events for the parent to listen and respond to. @Input is used for receiving data, not emitting. ngModel two-way binding is for form controls, not event emission. ViewChild is for referencing component instances, not communicating via events.