Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am rewriting a large application from silver-light to angularjs, while doing it, I am realizing that each of my controller js file is spanning 2000-3000 lines of code. All of my code is heavily dependent on scope variables. Wondering if there is any opportunity to split single controller js file into multiple js files? Any pointer would be appreciated.

share|improve this question

closed as too broad by PSL, gnat, greg-449, Mark Rotteveel, Lorenzo Dematté Sep 13 '14 at 15:50

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Services and factories should be your friends. You also mention that things rely upon a lot of scope variables. If something isn't bound to the DOM, I try not to hang it off the scope – NuclearGhost Sep 13 '14 at 5:06
    
Pramod, although it is spanning 2-3K, the code is modular; however 2-3K javascript in single file becomes nigthmare for maintenance and therefore was the question. Do let me know if there is a way to split controller into multiple files and share the scope. – Mothupally Sep 13 '14 at 5:40
    
something is definitely wrong in your code design – charlietfl Sep 13 '14 at 11:49

Use multiple controller to handle the task in your page. If using a larg controller is unavoidable ,you can split the definition to multiple files by passing the scope to another method and then define the rest of methods there.

In the first file:

app.controller('CtrlA', function($scope){
app.expandControllerA($scope);

});

In the second file

app.expandControllerA = function($scope)
{

}

You can pass any variable or dependencies to expandControllerA function.

share|improve this answer
    
This is an easy method to extend your controller without change your code! – Leopoldo Sanczyk May 22 '15 at 4:57

You could use App.factory or app.service in your angular module, They would be more like inhereting the data in your snippet. so that you can assign the part of data out tside the main controller and inherite the assigned variable in your controller.

    app.factory('name of the factory',function(){
return (data)
   )
app.controller('$scope','name of the factory', function(scope, name of the factopry

$scope.new = data;
))
share|improve this answer

You can also create a base controller and use $controller service to create derived controllers.

For example:

function BaseCtrl($scope, options) {
    var vm = this;

    // code
    return vm;
}

function ChildCtrl($controller, $scope) {
    var vm = $controller('BaseCtrl', {
        $scope: $scope,
        options: { }
    });

    // extend view model
    vm.name = '';

    return vm
}

There are other ways to split controllers into separate files, check out this ng-conf video.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.