AngularJS: Het verschil tussen Components en Directives

AngularJS: Het verschil tussen Components en Directives

AngularJS is een uitstekend framework om snel client-side apps mee te maken. Het MVC framework biedt controllers, services, factories, views, directives en componenten. Servies, controllers en views hebben een duidelijke functie binnen de app, maar components en directives hebben veel gemeen.

Belangrijkste kenmerken

Components

  • heeft een template
  • maakt een component (nieuw DOM element)
  • is technisch een directive

Directives

  • voegt gedrag toe aan een bestaand component (of ander DOM element)

Verschil

Het belangrijkste verschil tussen een component en directive is dat een component wordt gebruikt om templates te laden, je kunt de template voorzien van logica door bijvoorbeeld ng-click, ng-if en ng-submit directives te gebruiken.

Een Directive wordt gebruikt om gedrag toe te voegen aan een DOM element. Voorbeelden zijn; required-directive op een input element.

Directive Component
bindings Nee Ja
bindToController Ja Nee
compile function Ja Nee
controller Ja Ja
controllerAs Ja Ja
link functions Ja Nee
multiElement Ja Nee
priority Ja Nee
replace Ja Nee
require Ja Ja
restrict Ja Nee
scope Ja Nee
template Ja Ja
templateNamespace Ja Nee
templateUrl Ja Ja
terminal Ja Nee
transclude Ja Ja


Conclusie

Met de volgende vuistregel kun je altijd snel een keuze maken of je een component of directive moet maken; Gebruik component als je een element wilt maken (restricted to 'E'). Gebruik een directive als je een attribute wilt maken (restricted to 'A').

Reacties

Er zijn nog geen reacties, laat je reactie achter.

Laat een reactie achter

Lees ook

10 JavaScript one-liners every developer should know

Code should be readable and pragmatic. For me, that means when I look at a piece of code, I should almost instantly understand what it does. Clear code always has good variable names, is predictable, avoids unnecessary logic, and lives in the right place within a project. But sometimes you need to do something a little complex without turning the rest of your code into a mess. That’s where one-liners come in. Small, pragmatic snippets…

Continue reading

The difference between debounce and throttle

Debounce and throttle are powerful techniques, but only if you really understand what they do. I often ask about them in tech interviews, and to my surprise, many developers (even seniors) struggle to explain the difference. The good news is: it’s actually quite simple. In this post, I’ll break down both techniques and share some practical use cases. If you are unfamiliar with the techniques and are asked about them too in your interview…

Continue reading

The paradox of AI in web development

Since the start of my career as a developer, I’ve seen a lot of changes. From the rise of Dreamweaver with a WYSIWYG editor for generating table code to the arrival of jQuery and professional frameworks that completely transformed web development. Nowadays, there seems to be only one buzzword everywhere: AI.…

Continue reading