AngularJS seed: putting JavaScript into separate files (app.js, controllers.js, directives.js, filters.js, services.js)

后端 未结 3 1654
南笙
南笙 2020-12-12 13:16

I\'m using the angular-seed template to structure my application. Initially I put all my JavaScript code into a single file, main.js. This file contained my mod

相关标签:
3条回答
  • 2020-12-12 13:54

    If you're wanting to put your different parts of your application (filters, services, controllers) in different physical files, there are two things you have to do:

    1. Declare those namespaces (for lack of a better term) in your app.js or in each file;
    2. Refer to those namespaces in each of the files.

    So, your app.js would look like this:

    angular.module('myApp', ['external-dependency-1', 'myApp.services', 'myApp.controllers'])
    .run(function() {
       //...
    
    })
    .config(function() {
      //...
    });
    

    And in each individual file:

    services.js

    angular.module('myApp.services', []); //instantiates
    angular.module('myApp.services') //gets
    .factory('MyService', function() { 
      return {};
    });
    

    controllers.js

    angular.module('myApp.controllers', []); //instantiates
    angular.module('myApp.controllers')      //gets
    .controller('MyCtrl', function($scope) {
      //snip...
    })
    .controller('AccountCtrl', function($scope) {
      //snip...
    });
    

    All of this can be combined into one call:

    controllers.js
    angular.module('myApp.controllers', []) 
    .controller('MyCtrl', function($scope) {
     //snip...
    });    
    

    The important part is that you shouldn't redefine angular.module('myApp'); that would cause it to be overwritten when you instantiate your controllers, probably not what you want.

    0 讨论(0)
  • 2020-12-12 13:58

    The problem is caused from you "redeclaring" your application module in all your separate files.

    This is what the app module declaration (not sure declaration is the right term) looks like:

    angular.module('myApp', []).controller( //...
    

    This is what assignment (not sure if assignment is the right term either) to your application module looks like:

    angular.module('myApp').controller( //...
    

    Notice the lack of square brackets.

    So, the former version, one with the square brackets, should only be used once, usually in your app.js or main.js. All other associated files — controllers, directives, filters … — should use the latter version, the one without the square brackets.

    I hope that makes sense. Cheers!

    0 讨论(0)
  • 2020-12-12 14:09

    You get the error because you didn't define myApp.services yet. What I did so far is putting all the initial definitions in one file and then use them in another. Like for your example I would put in:

    app.js

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

    That should get rid of the error, though I think you should have a read on the article Eduard Gamonal mentioned in one of the comments.

    0 讨论(0)
提交回复
热议问题