简体   繁体   English

如何在没有路线的情况下使用AngularAMD

[英]How do I use AngularAMD without routes

I have an AngularAMD app, and I have a directive that depends (in a RequireJS sense) on that app (it is the Angular module in which that directive "lives"). 我有一个AngularAMD应用程序,我有一个指令(在RequireJS意义上)依赖于该应用程序(它是该指令“生活”的Angular模块)。

If I use routing, and use the directive inside a routed view, then there is a route that includes a controllerUrl, which depends on the file containing the directive. 如果我使用路由,并在路由视图中使用该指令,那么有一个包含controllerUrl的路由,它取决于包含该指令的文件。

If I don't, then... 如果我不这样做,那么......

Well, it's enragingly circular. 嗯,这是令人沮丧的循环。 If I tell the app that it depends on the directive, I get a circular dependency. 如果我告诉应用程序它依赖于指令,我会得到循环依赖。 If I don't, Angular doesn't know about the directive and the tag is just ignored. 如果我不这样做,Angular不知道该指令,并且标签被忽略。

Short of reproducing the entire controller-resolving mechanism from AngularAMD, is there a way I supposed to do this? 没有从AngularAMD再现整个控制器解析机制,我有没有办法做到这一点?

I think that there can be two cases: 我认为可能有两种情况:

1) Your directive is standalone and doesn't actually depend on anything in the application. 1)您的指令是独立的,实际上并不依赖于应用程序中的任何内容。

In this case you can put it into own module (both in terms of RequireJS and angular): 在这种情况下,您可以将它放入自己的模块中(包括RequireJS和angular):

// my-directive.js
var module = angular.module('my-directive', []);

module.directive('myDirective', [
  ...
]);

// app.js (depends on my-directive.js)
var app = angular.module('myapp', ['my-directive']);

This use case is supported by AngularAMD, see 3rd Party AngularJS Modules . AngularAMD支持此用例,请参阅第三方AngularJS模块

2) Your directive depends on something inside your app. 2)您的指令取决于您的应用程序内的内容。 In this case - put it into the same module (also both in terms of angular and RequireJS). 在这种情况下 - 将它放入同一模块中(同样在角度和RequireJS方面)。

// app.js
var app = angular.module('myapp', []);

app.directive('myDirective', [
  ...
]);

3) [Update] One more solution: AngularAMD also supports a special case to define and load an application wide module . 3)[更新]另一个解决方案:AngularAMD还支持定义和加载应用程序范围模块的特殊情况。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM