Directives Questions with Answers

19 practice questions on Directives (Angular) with answers and step-by-step solutions. Try 10 samples below, then practise all 19 free in the AptiNest app.

Sample questions

Question 1Easy

Which directive adds or removes CSS styles from an object of values?

  1. ngFor
  2. ngIf
  3. ngModel
  4. ngStyle

Answer: D. ngStyle

Explanation

[ngStyle]="{color: c}".

Step-by-step solution

  1. Style.

Why the other options are wrong

  • A. ngFor repeats elements; it does not change CSS styles.
  • B. ngIf adds or removes elements; it does not set styles.
  • C. ngModel binds form values; it does not set CSS styles.

Question 2Easy

What does *ngFor do?

  1. Repeats an element for each item in a collection
  2. Handles form submission events on the host
  3. Imports a standalone component into a template
  4. Creates a pipe

Answer: A. Repeats an element for each item in a collection

Explanation

It renders the host element once per item.

Step-by-step solution

  1. Loop over data.

Why the other options are wrong

  • B. ngFor repeats elements; it does not handle submit events.
  • C. Standalone imports go in an imports array, not through ngFor.
  • D. Pipes are declared with @Pipe; ngFor only repeats elements.

Question 3Easy

Which of these is a built-in structural directive?

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

Answer: D. *ngIf

Explanation

Structural directives change the DOM layout by adding or removing elements, written with an asterisk.

Step-by-step solution

  1. Star syntax indicates structural.

Why the other options are wrong

  • A. ngClass is an attribute directive.
  • B. ngSubmit is an event binding, not a structural directive.
  • C. ngModel is a form directive.

Question 4Easy

What is <ng-container> mainly used for?

  1. Defining a lazy-loaded route inside a template block
  2. Grouping elements without adding an extra DOM element
  3. Declaring a service provider directly in the template
  4. Wrapping content in a block-level div so that it can be styled

Answer: B. Grouping elements without adding an extra DOM element

Explanation

ng-container renders no element of its own, so it adds no extra markup.

Step-by-step solution

  1. Ng-container is a grouping element for template content.
  2. It renders no element of its own, so it adds no extra markup.
  3. The correct answer is B (Grouping elements without adding an extra DOM element).

Why the other options are wrong

  • A. Lazy routes are defined in the router configuration, not in a template.
  • C. Providers go in a providers array, not in a template.
  • D. Ng-container adds no div, so it is not used to wrap content for styling.

Tip: Use ng-container when you need a structural directive but no extra wrapper element.

Question 5Medium

How do you write a custom attribute directive that reacts to host element events?

  1. Use @Output on the host element and bind it in the template
  2. Attach jQuery handlers in ngOnInit of the parent component
  3. Use @HostListener inside a class decorated with @Directive
  4. Extend the Component decorator and override its template

Answer: C. Use @HostListener inside a class decorated with @Directive

Explanation

@HostListener('mouseenter') binds a method to a DOM event on the host element.

Step-by-step solution

  1. Use @Directive to create a custom directive class.
  2. Add @HostListener decorator to bind an event to the host element.
  3. Write a method in the class that reacts to the event.
  4. The method will execute when the event occurs on the host.
  5. This allows the directive to respond to DOM events directly.

Why the other options are wrong

  • A. Output is for child-to-parent events, not for listening to the host element's own events.
  • B. jQuery handlers in a parent's ngOnInit do not make a directive react to its host element.
  • D. Extending the Component decorator is not how to build a directive; use @Directive instead.

Tip: @HostListener is used in directives to handle events on the host element.

Question 6Medium

Which attribute-binding form sets the DOM property disabled of a button from a component field busy?

  1. (disabled)="busy"
  2. {disabled}="busy"
  3. [disabled]="busy"
  4. #disabled="busy"

Answer: C. [disabled]="busy"

Explanation

Square brackets denote property binding from component to element.

Step-by-step solution

  1. The question asks for a binding that sets the DOM property disabled.
  2. Angular sets DOM properties with square brackets around the property name.
  3. The form [disabled]="busy" sets the button's disabled property from the busy field. The correct answer is C ([disabled]="busy").

Why the other options are wrong

  • A. Round brackets bind events, not properties.
  • B. Curly braces are not Angular binding syntax.
  • D. A hash creates a template reference variable, not a property binding.

Tip: [ ] sets a property, ( ) listens for an event, and [( )] binds two ways.

Question 7Medium

What is the purpose of ng-container?

  1. Declare a service that every component in the template tree can then inject directly
  2. Group elements for a structural directive without adding an extra DOM element
  3. Load a lazy module when the route is hit
  4. Style a list with a theme

Answer: B. Group elements for a structural directive without adding an extra DOM element

Explanation

It renders nothing itself, only its children.

Step-by-step solution

  1. Avoid extra wrapper elements.

Why the other options are wrong

  • A. ng-container groups content; it does not declare a service.
  • C. Lazy loading is set up in the routes, not by ng-container.
  • D. ng-container adds no styling; it groups content without DOM output.

Question 8Medium

How are [ngClass] and class bindings different?

  1. [ngClass] only works on components, never on plain DOM elements like div
  2. [ngClass] removes all static classes from the element
  3. [ngClass] can toggle several classes from an object, array or string
  4. There is no difference between them

Answer: C. [ngClass] can toggle several classes from an object, array or string

Explanation

ngClass takes a map of class names to booleans.

Step-by-step solution

  1. Object form: {active: isOn}.

Why the other options are wrong

  • A. ngClass works on plain DOM elements such as div as well.
  • B. ngClass adds and removes classes; it keeps static classes intact.
  • D. They differ: ngClass takes objects, arrays or strings for many classes.

Question 9Hard

Why can't you place *ngIf and *ngFor on the same element?

  1. Only one structural directive is allowed per element; nest with ng-container
  2. Both can sit on one element, but ngFor always runs first and wins
  3. ngIf overrides ngFor at runtime, so the loop never renders anything
  4. Structural directives must be declared in a module of their own first

Answer: A. Only one structural directive is allowed per element; nest with ng-container

Explanation

The * syntax wraps the element in an ng-template; two on one element would need two wrappers, so nest with <ng-container>.

Step-by-step solution

  1. The * syntax wraps an element in an ng-template.
  2. Two structural directives on one element would need two wrappers.
  3. Angular allows only one per element, so nest them with ng-container. The correct answer is A.

Why the other options are wrong

  • B. NgFor does not run first and win; putting both on one element is not allowed.
  • C. NgIf does not override ngFor at runtime; the rule is about the syntax.
  • D. Modules are not the reason; the limit applies to the element.

Tip: Put *ngIf and *ngFor on separate elements, grouping with ng-container if needed.

Question 10Hard

In a custom attribute directive, which decorator lets you react to host element events?

  1. @Input() for values passed from the parent
  2. @ViewChild() for querying a template element
  3. @Injectable() for registering a service
  4. @HostListener

Answer: D. @HostListener

Explanation

@HostListener('click') subscribes to events on the host element.

Step-by-step solution

  1. Need to listen to host events.

Why the other options are wrong

  • A. Input receives values from the parent; it does not listen to host events.
  • B. ViewChild queries template elements; it does not handle host events.
  • C. Injectable marks services.

Practise all 19 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Angular topics · All topics