AngularJS:服务在不同的文件中 [英] AngularJS: Service in different file

查看:61
本文介绍了AngularJS:服务在不同的文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟随ng-boilerplate启发的组织学习AngularJS.我为网站的不同部分创建了不同的Angular模块.

I'm learning AngularJS following an organization inspired by ng-boilerplate. I create different Angular modules for the different parts of my site.

但是,我想在主模块下创建所有通用元素(服务和指令),同时将它们都放在单独的源文件中.

However, I want to create all common elements (services and directives) under the main module, while having them all be in separate source files.

此代码有效,但是sessionService.js中的模块是否引用了与app.js相同的模块,还是创建了一个具有相同名称的新模块?

This code works, but is the module in sessionService.js referencing the same module than app.js, or is it creating a new one with the same name?

app.js

var app = angular.module('myApp', [...])
.config(...)
.controller(...);

sessionService.js

angular.module('myApp', [])
.service('SessionService', function() { ... });

推荐答案

如果在同一页面上多次调用angular.module('myApp', []),则可能会遇到错误或冲突.我从未尝试过.

If you call angular.module('myApp', []) multiple times on the same page, you will likely run into errors or conflicts. I never tried that.

但是,如果您已经运行一次angular.module('myApp', []).然后,您可以运行angular.module('myApp')(注意:没有[])来检索(引用)您先前定义的myApp模块.

However, if you already run angular.module('myApp', []) once. Then you can run angular.module('myApp') (note: without []) to retrieve (refer to) the myApp module you defined earlier.

这篇关于AngularJS:服务在不同的文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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