Prepare for Angular Interviews with real-life questions. Utilize quizzes and examples to deepen understanding and enhance your skills. Gear up to ace your interview!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


Which decorator is used to create a custom directive in Angular?

  1. @Component

  2. @Injectable

  3. @Directive

  4. @NgModule

The correct answer is: @Directive

The use of the @Directive decorator is essential in Angular for creating custom directives. Directives are a powerful feature in Angular that allow you to extend the capabilities of HTML elements by attaching behavior to them. When you define a directive with the @Directive decorator, it enables the directive to manage elements in the DOM. This includes controlling their appearance, behavior, or transforming them based on certain conditions. The decorator allows you to specify the selector that determines when the directive should be applied, as well as any metadata required for the directive’s functionality. In contrast, other decorators serve different purposes within Angular. The @Component decorator is specifically for creating components, which are the building blocks of an Angular application that encompass both the view and behavior. The @Injectable decorator is used for services, marking them as available for dependency injection. The @NgModule decorator is used to define a module that encapsulates components, directives, services, and other code related to a specific application feature, but it is not used for creating directives themselves. Thus, using @Directive is the correct way to create a custom directive, allowing developers to enhance and customize the behavior of elements within their Angular applications.