是否可以在没有模块的情况下创建 AngularJS 服务? [英] Is it possible to create an AngularJS service without a module?
问题描述
在 AngularJS 中有一个简短的表单来创建控制器,因此您不必为此使用模块.这个简短的形式就是简单地定义一个函数
function FooController ($scope) {//...}
并使用 ng-controller
从 HTML 标记中引用它:
...
现在,我的问题是,是否有类似的简短形式"来定义服务?换句话说:你能在不使用模块的情况下定义(和使用)服务吗?
请注意,我知道使用模块构建应用程序是完全有意义的,不这样做可能/应该被视为不好的做法.只是我正在向完全不熟悉 AngularJS 的人解释 AngularJS,今天就出现了这个问题.所以这只是为了好奇和完整性.
要注册服务,您必须有一个模块,该服务将成为其中的一部分.
所以我想答案是否定的.
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.
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.
这篇关于是否可以在没有模块的情况下创建 AngularJS 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!