xavier
xavier

Reputation: 2039

Directives in Angular: why are they useful? Can't they just be substituted by (event)s?

I'm new to Angular2 and I'm reading the differences between elements, components, directives, etc. and I don't understand why @Directive exist.

I already read the documentation (it's not necessary to link it, thanks, I already checked it) and I've seen that @Component is a particular case of a directive. Also, there are different kinds of directives (Structural, Attributes and the already mentioned Components). However, at the end of the day, I look at some examples of @Directive and I find they can be substituted by an (event) in the corresponding @Component.

Example: the main example of a @Directive in this page @Directive v/s @Component in Angular could be substituted with something similar to

<contact-card [name]="'foo'" [city]="'bar'" (onClick)="someAction()"></contact-card>

Perchance I didn't understand it properly and it is not possible to substitute it, but it case it is, is there any good reason or useful examples where using @Directive is actually necessary or simpler than tweaking a @Component?

Thanks in advance!

Upvotes: 1

Views: 101

Answers (1)

Seba Cherian
Seba Cherian

Reputation: 1793

Directives add behaviour to an existing DOM element or an existing component instance.

A component, rather than adding/modifying behaviour, actually creates its own view (hierarchy of DOM elements) with attached behaviour.

Refer the following documentations:

Directive

Component

Upvotes: 1

Related Questions