是否可以在没有模块的情况下创建AngularJS服务? [英] Is it possible to create an AngularJS service without a module?

查看:102
本文介绍了是否可以在没有模块的情况下创建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) {
  // ...
}

并使用ng-controller从HTML标记中引用它:

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?

请注意,我知道使用模块构造应用程序是很有意义的,否则,应该/应该将其视为不良做法.只是我向一个完全陌生的人解释了AngularJS,而今天的问题浮出水面.因此,这只是出于好奇和完整性.

推荐答案

引自创建服务的文档

要注册服务,您必须具有该服务将要使用的模块 成为其中的一部分.

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆