What is the difference between directives (Directive) and components (Component)?

Asked

Viewed 2,370 times

7

It seems that in more recent versions, in addition to the method directive, Angular 1 also has a method called component.

I would like to understand the main differences between one and the other.

What are the main differences between a directive and a component?

When should I wear component instead of a directive?

Before you ask me this: The documentation page is in English, I would like an explanation in Portuguese, which for me is easier.

2 answers

2

The Component is a special Directive, it was created to address and correct problems that Directive has when Voce wants to create an html component that has a controller and an html itself, these problems would be: does not have bindings, does not have isolated Scope and ambiguities generated by the specifications of the Directives of type link and attribute.

You must use a Component: Whenever you want to create an isolated scopo html component (replace creating an html with Binding via ng-controller from a controller).

You must use a Directive: Only when you want to create an attribute that performs some javascript of DOM manipulation or simple manipulation of information.

0

The component is similar to the directive, but it is easier to configure and comes with the tendency to facilitate the migration of projects from Angular 1.x to Angular 2. However, the components are limited to attributes only.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.