Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Q: For example I have a module in my App and I have directives written for it, And I want to add few more modules to my app and also want to reuse existing directives written for another module. How I can achieve that. Not only modules that applies to filters, config etc..</p> </blockquote> <p>When you declare a module, you specify its dependencies. So if you have a directive like this:</p> <pre class="lang-js prettyprint-override"><code>angular.module( 'moduleA', [] ) .directive( 'myDirective', function () { // ... }); </code></pre> <p>You can require that module from another module:</p> <pre class="lang-js prettyprint-override"><code>angular.module( 'moduleB', [ 'moduleA' ] ); </code></pre> <p>The array argument to <code>angular.module</code> is a list of dependencies.</p> <blockquote> <p>Q: Can I have sub modules defined inside a Module?</p> </blockquote> <p>Modules are technically all independent, but it's super common to fake it. So we can define <code>moduleA</code> with it's "submodules" like so:</p> <pre class="lang-js prettyprint-override"><code>angular.module( 'moduleA.one', [] ); angular.module( 'moduleA.two', [] ); angular.module( 'moduleA', [ 'moduleA.one', 'moduleA.two' ]); </code></pre> <p>And it's clear to the developer/reader that <code>moduleA.one</code> and <code>moduleA.two</code> are part of <code>moduleA</code>, but also anytime another module depends on <code>moduleA</code>, its two submodules will be included as well.</p> <p>But technically, these are all independent, so <code>moduleB</code> could also require <code>moduleA.two</code> if it wanted to do so.</p> <blockquote> <p>Q: How can I add a controller to an element dynamically, it should not be static, i.e through html markup ng-controller.</p> </blockquote> <p>This is probably not a good idea. The "view" is the official record. What's your use case?</p> <blockquote> <p>Q: If I want to share a thing across all modules how can I do that.. For example I have a variable defined outside of all modules in my app and I just want to access them inside modules.. Is that possible, I have this doubt because it completely works on individual scopes, shared scope and rootScope etc..</p> </blockquote> <p>I am not sure I understand what you're asking. But when you define something in a module, you can access it from any other just by requiring it, as discussed above. But in an AngularJS app, there should be <em>anything</em> "outside of all modules" - everything should be in a module because otherwise it'ts defined in the global scope (e.g. <code>window</code>) and that's just bad practice.</p> <hr> <p>To get a better understanding of how modules can be structured to great benefit, check out my <code>ngBoilerplate</code> kickstarter: <a href="http://ngbp.github.io/ngbp/">http://ngbp.github.io/ngbp/</a>. The module structure is specifically designed for code reuse and demonstrates many of these concepts in practice.</p> <p>Feel free to elaborate on any of these and I will revise the responses.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload