Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does $compileProvider have a directive method?

I observe from the Angular documentation, that $compileProvider has both a directive() and component() method. See: https://docs.angularjs.org/api/ng/provider/$compileProvider

I also observe the usage of that directive in the docs https://docs.angularjs.org/api/ng/service/$compile

Search for: 'compileExample'

But I don't see any explanation of the purpose.

Questions

Q) Why does $compileProvider have a directive method? What's the purpose or benefit?

Q) How is registering a directive with the compiler different from declaring a directive on a module?

like image 573
Michael R Avatar asked Feb 24 '26 22:02

Michael R


1 Answers

How is registering a directive with the compiler different from declaring a directive on a module?

The module loader uses the $compileProvider.directive() method to load directives:

AngularJS Module Loader Source Code

/**
   * @ngdoc method
   * @name angular.Module#directive
   * @module ng
   * @param {string|Object} name Directive name, or an object map of directives where the
   *    keys are the names and the values are the factories.
   * @param {Function} directiveFactory Factory function for creating new instance of
   * directives.
   * @description
   * See {@link ng.$compileProvider#directive $compileProvider.directive()}.
   */
  directive: invokeLaterAndSetModuleName('$compileProvider', 'directive'),

The difference is that the module loader is capable of decorating directives and does it before the config phase. I have not found a use case for adding directives during the config phase.

like image 71
georgeawg Avatar answered Feb 27 '26 16:02

georgeawg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!