N
N
No Name2018-06-17 20:51:04
Angular
No Name, 2018-06-17 20:51:04

What is the difference between a directive and a component in Angular and when to use which?

What is the difference between a directive and a component in Angular and when to use which?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lega, 2018-06-17
@lega

The component has its own template and works with it (as an isolated sub-application), the directive does not have a template (although there is a possibility) and is placed on someone else's template to change behavior.

V
Vladimir, 2018-06-18
@Casufi

If we are talking about the second angular, then here, in fact, everything is there - directives, only different and the purpose is consistent with the description
https://angular.io/guide/attribute-directives
There are three kinds of directives in Angular:
Components—directives with a template.
Structural directives—change the DOM layout by adding and removing DOM elements.
Attribute directives—change the appearance or behavior of an element, component, or another directive.
If the first one, there components appeared after the directives, and their functionality overlaps a bit, but there is also a good explanation of when a component should be preferred.
https://docs.angularjs.org/guide/component
Advantages of Components:
simpler configuration than plain directives
promote sane defaults and best practices
optimized for component-based architecture
writing component directives will make it easier to upgrade to Angular
When not to use Components:
for directives that need to perform actions in compile and pre- link functions, because they aren't available
when you need advanced directive definition options like priority, terminal, multi-element
when you want a directive that is triggered by an attribute or CSS class, rather than an element

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question