What is the difference between component and directive in Angular 2?

Josh Long

I've been struggling to understand what is the difference between these two concepts in the framework.

I'm well familiar with what directives in AngularJS 1.x are and both components and directives in Angular 2 seems quite similar to this concept...

Minko Gechev

You can think of any Component as a Directive with a View.

Consequences

Based on the fact that only components have views, there are a couple of consequences, for instance:

  • Only components may define directives to be used in the component itself and the entire subtree it is root of.
  • Only components may define pipes to be used in the component and the entire subtree it is root of.
  • Only components can define viewEncapsulation since they can have views, in contrast to directives
  • When the framework creates an ElementInjector for given component, it'll be marked as a Host injector.
  • A separate instance of the change detector is going to be created only for components (and respectively only components can define change detection strategy).

Further details

The classical way of defining component in Angular 2 is:

@Component({
  selector: '...',
  // ...
})
@View({
  template: '...'
})
class ComponentCtrl {...}

The @View decorator helps you define a view for given component. Initially it was externalized in a separate decorator (just like on the example above) because the Angular team plans to allow a single component to have multiple view definitions (one for each platform the component is going to work on). Recently this decorator was dropped, so currently you can define a component with:

@Component({
  selector: '...',
  template: '...',
  //...
})
class ComponentCtrl {...}

This way you achieve the same result but with a little bit less of typing. Internally Angular 2 will add the appropriate view metadata based on the properties you've set to the @Component decorator.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the difference between component and directive?

From Java

What's the difference between an Angular component and module

From Dev

Communication between custom directive and component in Angular2

From Dev

What is the analog of the 'controllerAs' directive's property in Angular 2 component?

From Dev

What is the difference between OnChanges and DoCheck in Angular 2?

From Dev

What is the difference between 'String' and 'Text' in Angular 2?

From Dev

Angular: what is the difference between declaring a service in a component and the bootstrap process

From Dev

Angular 2: what differences between comparison operators == and === in ngIf directive

From Dev

Programmatically tell the difference between routes in Angular 2 component

From Java

What is the difference between '@' and '=' in directive scope in AngularJS?

From Dev

rsync - What is the difference between using * and ** in the exclude directive?

From Dev

What's the difference between string and non-string key in angular directive scope?

From Dev

What is (click) in Angular 2, if not a directive?

From Dev

angular 2 append directive after current component

From Dev

Injection of dynamic directive/component in Angular 2

From Dev

How to access the Component on a Angular2 Directive?

From Dev

Injection of dynamic directive/component in Angular 2

From Java

What is the difference between parentheses, brackets and asterisks in Angular2?

From Dev

Angular 2: What is the exact difference between private and public attributes in components?

From Dev

What is the difference between ng-if and *ng-if in angular2

From Dev

What's the difference between these 2 Angular code snippets?

From Dev

What is the difference between @Inject and @Injectable in Angular 2 typescript

From Dev

What is the difference between Angular 2 Components and Web Components?

From Dev

What is the difference between angular js 2 starter project and CLI project

From Dev

what is the difference between >&2 and &>2

From Dev

What is the difference between a ractive template, partial and component

From Dev

What is the difference between passing component to declarations or to directives?

From Dev

What is the difference between a Component and a Service dependency?

From Dev

What is the difference between a UML node and a UML component?

Related Related

  1. 1

    What is the difference between component and directive?

  2. 2

    What's the difference between an Angular component and module

  3. 3

    Communication between custom directive and component in Angular2

  4. 4

    What is the analog of the 'controllerAs' directive's property in Angular 2 component?

  5. 5

    What is the difference between OnChanges and DoCheck in Angular 2?

  6. 6

    What is the difference between 'String' and 'Text' in Angular 2?

  7. 7

    Angular: what is the difference between declaring a service in a component and the bootstrap process

  8. 8

    Angular 2: what differences between comparison operators == and === in ngIf directive

  9. 9

    Programmatically tell the difference between routes in Angular 2 component

  10. 10

    What is the difference between '@' and '=' in directive scope in AngularJS?

  11. 11

    rsync - What is the difference between using * and ** in the exclude directive?

  12. 12

    What's the difference between string and non-string key in angular directive scope?

  13. 13

    What is (click) in Angular 2, if not a directive?

  14. 14

    angular 2 append directive after current component

  15. 15

    Injection of dynamic directive/component in Angular 2

  16. 16

    How to access the Component on a Angular2 Directive?

  17. 17

    Injection of dynamic directive/component in Angular 2

  18. 18

    What is the difference between parentheses, brackets and asterisks in Angular2?

  19. 19

    Angular 2: What is the exact difference between private and public attributes in components?

  20. 20

    What is the difference between ng-if and *ng-if in angular2

  21. 21

    What's the difference between these 2 Angular code snippets?

  22. 22

    What is the difference between @Inject and @Injectable in Angular 2 typescript

  23. 23

    What is the difference between Angular 2 Components and Web Components?

  24. 24

    What is the difference between angular js 2 starter project and CLI project

  25. 25

    what is the difference between >&2 and &>2

  26. 26

    What is the difference between a ractive template, partial and component

  27. 27

    What is the difference between passing component to declarations or to directives?

  28. 28

    What is the difference between a Component and a Service dependency?

  29. 29

    What is the difference between a UML node and a UML component?

HotTag

Archive