25

I am trying to define controllers in separate files, but I'm getting the error:

transactionsController not a function got undefined

File structure

I have added files in this sequence 1- common.js 2- transactions.js

Common.js In common files I have defined

var app = angular.module("spModule", ["ngMessages", "ui.bootstrap"]);

Transactions.js

angular.module('spModule').controller('transactionsController',
    ['$scope', '$http', function ($scope, $http) {} ]
);

HTML FIle

<body ng-app="spModule" ng-controller="transactionsController">
7
  • This should work fine. Are you sure you're loading the files in the correct order? The error looks strange? Is that really the error you got? Commented Jan 30, 2015 at 7:44
  • 3
    did you include the Transactions.js file in the HTML document? Commented Jan 30, 2015 at 7:45
  • i've noticed the errors from angular coming back slightly strange. This seems to definitely be a script include/ordering problem, post the entire HTML. There is also no point in specifying an app variable if you don't use it in that file either. Commented Jan 30, 2015 at 7:47
  • try $compile, or plunker Commented Jan 30, 2015 at 9:10
  • @AndreasArgelius not getting any error regarding file includes , Commented Jan 30, 2015 at 9:54

4 Answers 4

58

First, you should get rid of the global app variable. This is not necessary. Second, you have to understand the principle of the angular.module() method.

Using angular.module() with two parameters (e.g. angular.module('my-module', [])) would result in setting a new module with its corresponding dependencies. In contrast, when using angular.module('my-module') the corresponding module is looked up internally and returned to the caller (getting).

The means when you first define you application you might just create the following files and structure.

app.js

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

FirstController.js

angular.module('myApp').controller('FirstController', function () {});

SecondController.js

angular.module('myApp').controller('SecondController', function () {});

If you now include these files in your html document in this particularly order (at least app.js has to come first), your application works just fine with two separate controllers in two separate files.

Further readings

I can recommend the AngularJS Styleguide on modules for getting more ideas on this topic.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, it works!!!. Be sure the first time you write .module(one argument, second argument), Others controllers, .module(just one argument) . The first time, it creates an instance, and the other ones, it gets the instance!!
3

You Can put this controller in seprate file like mycontroller1.js

    

    app.controller('myController', ['$scope',function($scope)    
        {
        $scope.myValue='hello World'
        }])


Same like this you can create new js file 'myNewcontroller.js' and can put new controller :


    app.controller('myController2', ['$scope',function($scope)    
        {
        $scope.myValue='hello World'
        }])


Hope this will help you :) Cheers !!

Comments

3

You can do this stuff by creating modules. Create module and respective controllers. And inject that module to the main app module.

Transactions.js

(function() {
   'use strict';
   angular.module('tmodule', []);
})();

(function() {
   'use strict';
    angular.module('tmodule').controller('transactionsController', ['$scope', '$http', 
            function ($scope, $http){
    }]);

})();

Now inject the "tmodule" to your Common.js file-

  var app = angular.module("spModule", ["ngMessages", "ui.bootstrap","tmodule"]);

Comments

1

Load your common.js first. Move ng-app directive to <html> tag. Change transaction.js to:

app.controller('transactionsController', TransactionsController)

TransactionsController.$inject = ['$scope','$http']

function TransactionsController($scope, $http) {


};

Just for fun. Let me know what happens.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.