简体   繁体   English

AngularAMD:应用程序取决于服务,但服务取决于应用程序

[英]AngularAMD: the app depends on services but services depend on the app

I am clearly missing something very basic. 我显然缺少一些非常基本的东西。

The instructions are to create an app, like this: 说明是要创建一个应用,如下所示:

define(['angularAMD'], function (angularAMD) {
    var app = angular.module(app_name, ['webapp']);
    ... // Setup app here. E.g.: run .config with $routeProvider
    return angularAMD.bootstrap(app);
});

And then create subsequent items like this: 然后创建后续项目,如下所示:

define(['app'], function (app) {
    app.factory('Pictures', function (...) {
        ...
    });
});

And there is this helpful line: 这条线很有帮助:

Any subsequent module definitions would simply need to require app to create the desired AngularJS services 任何后续模块定义都只需要要求应用程序即可创建所需的AngularJS服务

Well that's just great for subsequent module definitions, but app.config and app.run need lots of prerequisite modules that I am supposed to create -- as would any application beyond the level of a toy. 嗯,这对于后续的模块定义来说非常app.run ,但是app.configapp.run需要我应该创建的许多必备模块-玩具级别以外的任何应用程序也一样。 So there is obviously some simple solution that I am missing. 因此,显然有一些我缺少的简单解决方案。 How do I create services that the app depends on? 如何创建应用程序依赖的服务?

You can simply use 'angularAMD' injection to create services. 您可以简单地使用“ angularAMD”注入来创建服务。 For example, 例如,

define(['angularAMD'], function (angularAMD) {
  angularAMD.service('LoggerService',['$log',function($log){
     return function(msg){
        $log.log('message:', msg);
     }
  }]);

});

The services created using this method are available before the application is bootstrapped. 在启动应用程序之前,可以使用此方法创建的服务。 Hence app can depend on these services. 因此,应用程序可以依赖这些服务。

More similar code can be found at angular-AMD sample app. 可以在angular-AMD示例应用程序中找到更多类似的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在AngularAMD应用程序单元测试中模拟服务 - How to mock a service in AngularAMD app unit tests 是否可以在运行时使用angularAMD延迟加载角度模块并将其插入应用程序? - Is it possible to lazy-load and inject angular modules into the app in runtime using angularAMD? 使用requirejs,angularamd和karma时,“ undefined”不是对象(评估“ app.register.service”) - 'undefined' is not an object (evaluating 'app.register.service') when using requirejs, angularamd and karma angularAMD.bootstrap(app)行在IE10 IE11浏览器中引发异常,但在Google Chrome中有效 - angularAMD.bootstrap(app) line is throwing an exception in IE10 IE11 browsers but works in Google Chrome Angularamd在TypeScript控制器中注册 - Angularamd in typescript controller registering 如何缩小AngularAMD代码? - How to minify AngularAMD code? 如何使用业力/茉莉花在angularAMD中模拟服务? - How to mock service in angularAMD with karma/jasmine? AngularAMD和RequireJS,有时不加载AngularJS过滤器 - AngularAMD and RequireJS, sometimes not load AngularJS filter 使用angularAMD使用异步数据引导AngularJS服务 - bootstrap AngularJS service with asynchronous data using angularAMD 如何在没有路线的情况下使用AngularAMD - How do I use AngularAMD without routes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM