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

Tech interview tips for junior developers

A tech interview can feel intimidating, especially as a junior developer. You’ll be tested on knowledge, problem-solving, and teamwork—all while figuring out if the company and team are the right fit for you. This guide takes you step by step through preparation, what to expect, the questions you might face, what you can ask, and how to handle situations where you don’t know the answer. I’m focusing mainly on the technical interview,…

Continue reading

Mastering code reviews as junior developer

Pull requests, merge requests, and code reviews. You’ve probably heard these terms before. A code review is the moment developers ask each other for feedback. A bugfix or new feature is finished, and before it can go live, someone else takes a look. Even if the change works functionally, there’s often room to…

Continue reading

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