Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Directive vs @Component in Angular

Tags:

angular

What is the difference between @Component and @Directive in Angular? Both of them seem to do the same task and have the same attributes.

What are the use cases and when to prefer one over another?

like image 568
Prasanjit Dey Avatar asked Sep 20 '15 14:09

Prasanjit Dey


People also ask

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

Component is used to break up the application into smaller components. But Directive is used to design re-usable components, which is more behavior-oriented. That is why components are widely used in later versions of Angular to make things easy and build a total component-based model.

What is @directive in Angular?

What is meant by directives in Angular? Directives are classes that add new behavior or modify the existing behavior to the elements in the template. Basically directives are used to manipulate the DOM, for example adding/removing the element from DOM or changing the appearance of the DOM elements.

Is @component a directive?

A component is a directive with an associated view (i.e. HTML to be rendered). All components are directives, but not all directives are components. There are three types of directives: Component: A view with associated behaviour.

What is difference between component and module in Angular?

The Component is a fundamental block of Angular and multiple components will make up your application. A module can be a library for another module. For instance, the angular2/core library which is a primary Angular library module will be imported by another component.


1 Answers

A @Component requires a view whereas a @Directive does not.

Directives

I liken a @Directive to an Angular 1.0 directive with the option restrict: 'A' (Directives aren't limited to attribute usage.) Directives add behaviour to an existing DOM element or an existing component instance. One example use case for a directive would be to log a click on an element.

import {Directive} from '@angular/core';  @Directive({     selector: "[logOnClick]",     hostListeners: {         'click': 'onClick()',     }, }) class LogOnClick {     constructor() {}     onClick() { console.log('Element clicked!'); } } 

Which would be used like so:

<button logOnClick>I log when clicked!</button> 

Components

A component, rather than adding/modifying behaviour, actually creates its own view (hierarchy of DOM elements) with attached behaviour. An example use case for this might be a contact card component:

import {Component, View} from '@angular/core';  @Component({   selector: 'contact-card',   template: `     <div>       <h1>{{name}}</h1>       <p>{{city}}</p>     </div>   ` }) class ContactCard {   @Input() name: string   @Input() city: string   constructor() {} } 

Which would be used like so:

<contact-card [name]="'foo'" [city]="'bar'"></contact-card> 

ContactCard is a reusable UI component that we could use anywhere in our application, even within other components. These basically make up the UI building blocks of our applications.

In summary

Write a component when you want to create a reusable set of DOM elements of UI with custom behaviour. Write a directive when you want to write reusable behaviour to supplement existing DOM elements.

Sources:

  • @Directive documentation
  • @Component documentation
  • Helpful blog post
like image 142
jaker Avatar answered Oct 08 '22 20:10

jaker