When Making a Directive for AngularJS

Last Updated on July 27, 2018 by Colin

Worth Reading:  https://docs.angularjs.org/guide/directive

Especially this at the very bottom of that page:

Savvy readers may be wondering what the difference is between link and controller. The basic difference is that controller can expose an API, and link functions can interact with controllers using require. Best Practice: use controller when you want to expose an API to other directives. Otherwise use link.

By ‘expose an API’, they mean ‘add some this.doSomething() {…} functions to the controller that are reachable from outside the controller’.  Like public class methods.  If you’re not needing any of those, don’t add a controller to the directive.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.