javascript - 如何在应用程序中注入(inject)/加载 Angular 指令

标签 javascript angularjs

我的main.js位于应用程序的根文件夹中。

-app
 |_ routes.js
 |_ main.js
-components
 |_directives
   |_abc-directive.js

如何定义可以从另一个文件夹添加的指令。

我试过这个:

abc-directive.js:

var abcDirective = function() {
    // directive code
}

main.js:

app.main = angular.module('main', ['ngRoute', 'components.directives']);
app.main.directive('abcDirective', "<I don't want to define a directive here, 
                                      rather load from diff. folder>");

最佳答案

您可以在自己的模块中定义 Controller 、指令,并将这些模块添加为主模块的依赖项

abc-directive.js

var app = angular.module('directives.abcDirective', [])

app.directive('abcDirective', ...

ma​​in.js

var app = angular.module('mainApp', ['directives.abcDirective'])

您可以使用RequireJS管理依赖关系..

关于javascript - 如何在应用程序中注入(inject)/加载 Angular 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25361029/

相关文章:

javascript - 增加和减少 HTML 元素上的背景 RGBA 不透明度

php - 使用 Javascript 安全地查询 Solr

angularjs - 如何存储用户名并使其随处可见?

javascript - 在实例化指令 Controller 之前,如何扩充隔离指令范围?

angularjs - 集成 ASP.NET Web 窗体、Web API 和 AngularJS

javascript - 在过滤/搜索问题后更改输入 - VueJS

javascript - 如何获取具有公共(public)类的大量跨度的偏移值

javascript - 使用 ko.compulated 和 kendoMap 绑定(bind)动态添加标记到 kendoMap

javascript - 为什么 AngularJs 在 iPad 上(相对于桌面)慢很多?

javascript - AngularJS ng-repeat 和 bootstrap 以显示来自数据库的列中的元素