1

I have one application that has two pages.

1 - A list of users

2 - Where I can manage a particular user

In order to organize I create two separated controllers and one module inside the app.js file and put all stuff together (custom filters, directives, factories...)

Now I want to separate things in diferent files (or maybe folders too).

How can I do this?

angular.module('app', []).

controller('listController', function ($scope) {
    ... 
}).

controller('manageController', function ($scope) {
    ...
}).

factory('Api', ['$resource',
 function($resource) {
  return {
    ...
  };
}]).

filter('formtaData', function(){
   return function(data, modo){
       ...
   }
}).

Other issue is that my controllers have different dependencies and in the actual way I have to include them all in my module (and scripts on the page) even if a page don't need them.

3 Answers 3

2

appConfig.js

angular.module('app', [dep1, dep2]);

ListController.js

angular.module('app').controller('listController', function ($scope) {
    ... 
});

DetailsController.js

angular.module('app').controller('manageController', function ($scope) {
    ...
});

apiService.js

angular.module('app').factory('Api', ['$resource',
 function($resource) {
  return {
    ...
  };
}]);

formatDataFilter.js

angular.module('app').filter('formtaData', function(){
   return function(data, modo){
       ...
   }
});
Sign up to request clarification or add additional context in comments.

2 Comments

OK Ben, I understood. But I can put all filters together right? And do the same with the custom directives, and so on.... to avoid too many files.
You can, but it's better practice to put them each into their own file. In the end you will want to bundle and minimize your code into one file anyways, so organize your project so it's easy to understand.
1

You can do it in this way

templates/
    _login.html
    _list.html
    _manage.html
app/
    app.js
    controllers/
        ListController.js
        ManageController.js
    directives/
        SomeDirective.js
    services/
        ApiService.js
        AnotherService.js
    filters/
        FormtaDataFilter.js

In app.js you can write something like this:

app.config(function($routeProvider){
    $routeProvider
    .when('/list', {
        controller: 'ListController',
        templateUrl: 'templates/_list.html'
    })
    .when('/manage', {
        controller: 'ManageController.js',
        templateUrl: 'templates/_manage.html'
    })
    .otherwise({redirectTo: '/'});
});

2 Comments

OK Jorge, and I'll need to include this scripts inside the hml files right?
Your html files you can include in templates/ folder. I edit my answer to show you how to manage this
0

The above answer is correct however I usually assign the angular module to a literal to avoid typing the full name every time.

var angularApp = angular.module('app', [dep1,dep2]);

2 Comments

This is discouraged in pretty much every single Angular style guide. John Papa explains it well.
Thanks I did not know this I know now!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.