How do I write a custom module for AngularJS?

San Jay Falcon

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?

gion_13

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.

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.

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 */
});

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']);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I dynamically reload a module in a custom package?

来自分类Dev

How do I link to a pdf in AngularJS?

来自分类Dev

How do I check for browser support for AngularJS?

来自分类Dev

How do I write a Solr FunctionQuery to boost documents with future dates?

来自分类Dev

How do i write the integers that in this file to the mynumbers.txt file?

来自分类Dev

How do you I add a module into my PYTHONPATH?

来自分类Dev

How do i bind an "Entry Cell" to a custom object using Xamarin?

来自分类Dev

How do I get `sbt test` to recognize my custom target?

来自分类Dev

How to write a Rust compiler plugin that generates a module?

来自分类Dev

How do I set up a custom navbar and a custom collapse? The right does not line up in mobile

来自分类Dev

How do I write a shell script to get a given process idle time?

来自分类Dev

Given the function `decode`, how can I do the inverse and write a function to `encode`?

来自分类Dev

When using (substack's) Tape module for testing, how do I run only one test in a file?

来自分类Dev

How do you initialize and present a custom SLComposeServiceViewController?

来自分类Dev

How to do a custom transition using Javascript

来自分类Dev

How can I debug a custom transformer

来自分类常见问题

How do I resolve ClassNotFoundException?

来自分类Dev

How can I write key-value pairs in Prolog?

来自分类Dev

How to use from import if I would like to import the module as a String?

来自分类Dev

How to write a class that may accept a function pointer and/or functor just like a smart pointer does for custom deleter?

来自分类Dev

Can I prevent ng-repeat from html encoding / escaping my content in AngularJS 1.2? Or need to write own directive?

来自分类Dev

VB.NET Custom Listview - How do you get the imagelist?

来自分类Dev

How can I add a custom Parameter to a box in maxscript?

来自分类Dev

How do i sort my listview alphabetically?

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

How do I override a default keybinding in LightTable?

来自分类Dev

How do I modularize polyfills in Angular?

来自分类Dev

How do I declare a driver as global?

来自分类Dev

How do I combine lenses and functors?

Related 相关文章

  1. 1

    How do I dynamically reload a module in a custom package?

  2. 2

    How do I link to a pdf in AngularJS?

  3. 3

    How do I check for browser support for AngularJS?

  4. 4

    How do I write a Solr FunctionQuery to boost documents with future dates?

  5. 5

    How do i write the integers that in this file to the mynumbers.txt file?

  6. 6

    How do you I add a module into my PYTHONPATH?

  7. 7

    How do i bind an "Entry Cell" to a custom object using Xamarin?

  8. 8

    How do I get `sbt test` to recognize my custom target?

  9. 9

    How to write a Rust compiler plugin that generates a module?

  10. 10

    How do I set up a custom navbar and a custom collapse? The right does not line up in mobile

  11. 11

    How do I write a shell script to get a given process idle time?

  12. 12

    Given the function `decode`, how can I do the inverse and write a function to `encode`?

  13. 13

    When using (substack's) Tape module for testing, how do I run only one test in a file?

  14. 14

    How do you initialize and present a custom SLComposeServiceViewController?

  15. 15

    How to do a custom transition using Javascript

  16. 16

    How can I debug a custom transformer

  17. 17

    How do I resolve ClassNotFoundException?

  18. 18

    How can I write key-value pairs in Prolog?

  19. 19

    How to use from import if I would like to import the module as a String?

  20. 20

    How to write a class that may accept a function pointer and/or functor just like a smart pointer does for custom deleter?

  21. 21

    Can I prevent ng-repeat from html encoding / escaping my content in AngularJS 1.2? Or need to write own directive?

  22. 22

    VB.NET Custom Listview - How do you get the imagelist?

  23. 23

    How can I add a custom Parameter to a box in maxscript?

  24. 24

    How do i sort my listview alphabetically?

  25. 25

    How do I parse a RestSharp response into a class?

  26. 26

    How do I override a default keybinding in LightTable?

  27. 27

    How do I modularize polyfills in Angular?

  28. 28

    How do I declare a driver as global?

  29. 29

    How do I combine lenses and functors?

热门标签

归档