我怎样写AngularJS自定义模块? [英] How do I write a custom module for AngularJS?

查看:104
本文介绍了我怎样写AngularJS自定义模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写AngularJS自定义模块,但我找不到任何关于这个问题的好文档。我怎样写,我可以与他人分享AngularJS自定义模块?

I need to write a custom module for AngularJS, but I can't find any good documentation on the subject. How do I write a custom module for AngularJS that I can share with others?

推荐答案

在这种情况下是你认为的文档不能帮你了,要学会一个很好的办法就是看看其他业已构建模块和看看别人是怎么做的,他们是如何设计的架构,以及他们如何整合他们在他们的应用程序。结果
看着别人做过的事情后,你应该至少有一个起点。

In these situations were you think that the docs can't help you any more, a very good way to learn is to look at other already-build modules and see how others did it, how they designed the architecture and how they integrated them in their app.
After looking at what others did, you should have at least a starting point.

例如,看看任何角度UI模块,然后你会看到许多自定义模块。结果
一些定义只是一个单一的指令,而其他定义的more东西

For example, take a look at any angular ui module and you will see many custom modules.
Some define just a single directive, while others define more stuff.

@nXqd 表示,要创建一个模块的基本方式是:

Like @nXqd said, the basic way to create a module is:

// 1. define the module and the other module dependencies (if any)
angular.module('myModuleName', ['dependency1', 'dependency2'])

// 2. set a constant
    .constant('MODULE_VERSION', '0.0.3')

// 3. maybe set some defaults
    .value('defaults', {
        foo: 'bar'
    })

// 4. define a module component
    .factory('factoryName', function() {/* stuff here */})

// 5. define another module component
    .directive('directiveName', function() {/* stuff here */})
;// and so on

在定义模块后,它很容易将组件添加到它(还没有到您的模块存储在一个变量):

After defining your module, it's very easy to add components to it (without having to store your module in a variable) :

// add a new component to your module 
angular.module('myModuleName').controller('controllerName', function() {
    /* more stuff here */
});

和整合的部分是相当简单:只需添加它作为您的应用程序模块的依赖关系(的这里的如何角UI做的话)。

And the integration part is fairly simple: just add it as a dependency on your app module (here's how angular ui does it).

angular.module('myApp', ['myModuleName']);

这篇关于我怎样写AngularJS自定义模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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