Search code examples
angularjsangularjs-service

Is it possible to create an AngularJS service without a module?


In AngularJS there is a short form to create controllers, so you do not have to use a module for this. This short form is to simply define a function

function FooController ($scope) {
  // ...
}

and reference it from the HTML markup using ng-controller:

<div ng-controller="FooController">
  ...
</div>

Now, my question is, whether there is a similar "short form" to define services? In other words: Can you define (and use) a service without using a module?

Please note that I know that it perfectly makes sense to structure your applications using modules, and not doing so could / should be regarded as bad practice. It's just that I am explaining AngularJS to someone who's completely new to it, and the question arose today. So it's just for curiosity and completeness.


Solution

  • Quoted from the doc of Creating Services

    To register a service, you must have a module that this service will be part of.

    So I guess the answer is no.