Question Mark in Directive Require

It’s exactly what you guessed: ? makes a directive optional.

Basically, these are at your disposal when defining directive requirements:

  1. someDirective : Require someDirective on same element and pass it to linking function
  2. ?someDirective : Pass someDirective controller if available on same element to linking function. If not, pass null.
  3. ^someDirective : Require someDirective on one of the parent elements and pass it to linking function.
  4. ?^someDirective : Pass someDirective controller if available on one of parent elements to linking function. If not, pass null.

If your directive requires multiple other directives, you can use the same thing but pass an array like so:

require: ['firstRequiredDirective', '^secondRequiredDirective']

This time, you will get an array of required directive’s controllers passed to your linking function.

In your case, if element having your contenteditable directive has ngModel, ngModelController will get passed to your linking function.

If there is no ngModel directive on it, it will pass null.

Leave a Comment