tt9
tt9

Reputation: 6069

Specifying the host element type on an Angular Directive

I am building a directive in Angular (2+) and I want to limit the directive to which type of element it can be applied to.

Is there a built in Angular mechanism to specify that my directive can only be applied to an <input /> tag? Or will I have to specifically check the element reference?

Upvotes: 4

Views: 1801

Answers (1)

Jota.Toledo
Jota.Toledo

Reputation: 28434

As already mentioned in the comments, you can archive this by using a restrictive selector in the metadata of the directive:

@Directive({
   ...
   selector: 'input[fooDirective]',
   exportAs: 'fooDir'
   ...
})
export class FooDirective {
}

Upvotes: 5

Related Questions