• Overview
@angular/core

Directive

decorator

Decorator that marks a class as an Angular directive. You can define your own directives to attach custom behavior to elements in the DOM.

  
    @Directive ({})
  
  

selector

string | undefined

The CSS selector that identifies this directive in a template and triggers instantiation of the directive.

Declare as one of the following:

  • element-name: Select by element name.
  • .class: Select by class name.
  • [attribute]: Select by attribute name.
  • [attribute=value]: Select by attribute name and value.
  • :not(sub_selector): Select only if the element does not match the sub_selector.
  • selector1, selector2: Select if either selector1 or selector2 matches.

Angular only allows directives to apply on CSS selectors that do not cross element boundaries.

For the following template HTML, a directive with an input[type=text] selector, would be instantiated only on the <input type="text"> element.

          
<form>  <input type="text">  <input type="radio"><form>

inputs

(string | { name: string; alias?: string | undefined; required?: boolean | undefined; transform?: ((value: any) => any) | undefined; })[] | undefined

Enumerates the set of data-bound input properties for a directive

Angular automatically updates input properties during change detection. The inputs property accepts either strings or object literals that configure the directive properties that should be exposed as inputs.

When an object literal is passed in, the name property indicates which property on the class the input should write to, while the alias determines the name under which the input will be available in template bindings. The required property indicates that the input is required which will trigger a compile-time error if it isn't passed in when the directive is used.

When a string is passed into the inputs array, it can have a format of 'name' or 'name: alias' where name is the property on the class that the directive should write to, while the alias determines the name under which the input will be available in template bindings. String-based input definitions are assumed to be optional.

outputs

string[] | undefined

Enumerates the set of event-bound output properties.

When an output property emits an event, an event handler attached to that event in the template is invoked.

The outputs property defines a set of directiveProperty to alias configuration:

  • directiveProperty specifies the component property that emits events.
  • alias specifies the DOM property the event handler is attached to.

providers

Provider[] | undefined

Configures the injector of this directive or component with a token that maps to a provider of a dependency.

exportAs

string | undefined

Defines the name that can be used in the template to assign this directive to a variable.

queries

{ [key: string]: any; } | undefined

Configures the queries that will be injected into the directive.

Content queries are set before the ngAfterContentInit callback is called. View queries are set before the ngAfterViewInit callback is called.

host

{ [key: string]: string; } | undefined

Maps class properties to host element bindings for properties, attributes, and events, using a set of key-value pairs.

Angular automatically checks host property bindings during change detection. If a binding changes, Angular updates the directive's host element.

When the key is a property of the host element, the property value is propagated to the specified DOM property.

When the key is a static attribute in the DOM, the attribute value is propagated to the specified property in the host element.

For event handling:

  • The key is the DOM event that the directive listens to. To listen to global events, add the target to the event name. The target can be window, document or body.
  • The value is the statement to execute when the event occurs. If the statement evaluates to false, then preventDefault is applied on the DOM event. A handler method can refer to the $event local variable.

jit

true | undefined

When present, this directive/component is ignored by the AOT compiler. It remains in distributed code, and the JIT compiler attempts to compile it at run time, in the browser. To ensure the correct behavior, the app must import @angular/compiler.

standalone

boolean | undefined

Angular directives marked as standalone do not need to be declared in an NgModule. Such directives don't depend on any "intermediate context" of an NgModule (ex. configured providers).

More information about standalone components, directives, and pipes can be found in this guide.

hostDirectives

(Type<unknown> | { directive: Type<unknown>; inputs?: string[] | undefined; outputs?: string[] | undefined; })[] | undefined

Standalone directives that should be applied to the host whenever the directive is matched. By default, none of the inputs or outputs of the host directives will be available on the host, unless they are specified in the inputs or outputs properties.

You can additionally alias inputs and outputs by putting a colon and the alias after the original input or output name. For example, if a directive applied via hostDirectives defines an input named menuDisabled, you can alias this to disabled by adding 'menuDisabled: disabled' as an entry to inputs.

Jump to details