0

I have just started developing with AngularJS a few days ago, and this issue really bugs me.

I keep getting this error:

Error: ng:areq
Bad Argument
Argument 'NewStudentCtrl' is not a function, got undefined

All other controllers are working since I am using them in other files, just NewStudentCtrl won't work. I have tried a lot of different things, only one worked: defining the controller using function NewStudentCtrl ($scope) {} inside the HTML file itself right before the use of the controller. The problem is that I want to split HTML and JS into seperate files.

Please note that the provided source is simplified a lot, there might be little indentation or syntax errors.

<html ng-app="myApp">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
  <script>var app = angular.module('myApp',[]);</script>
</head>
<body>
  <div ng-controller="NewStudentCtrl">
    <div ng-controller="AccountMenuCtrl">
    </div>

    <script src="js/account-menu.js">
      function AccountMenuCtrl ($scope) {
    }
    </script>

    <div ng-controller="OrtsteileCtrl">
        <option value="{{ortsteil.ID}}" ng-repeat="ortsteil in ortsteile">
          {{ortsteil.Name}}
        </option>
    </div>

    </div> <!-- end NewStudentCtrl -->

  <!-- Loading dependencies -->
  <script src="js/jquery-1.8.3.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/new-student.js">
    var app = angular.module("myApp");

    function NewStudentCtrl ($scope) {
    }
  </script>
  <script src="js/plz.js">
    angular.module('myApp').controller('OrtsteileCtrl', ['$scope', '$http',
      function($scope, $http) {

    }]);
  </script>
</body>
</html>
4
  • where have you defined NewStudentCtrl ? Commented Mar 1, 2014 at 10:19
  • In this simplified version, the definition is at line 30. Commented Mar 1, 2014 at 10:21
  • Why aren't you defining it like you define OrtsteileCtrl ? Commented Mar 1, 2014 at 10:32
  • I have tried that, it didn't work either. I then sticked to the functionsyntax because that worked HTML-inlined. Commented Mar 1, 2014 at 11:05

1 Answer 1

1

EDIT: Scratch that, something I didn't realise is that angular is happy with mixing them.

The issue is because you are recreating your module several times:

Line 4: <script>var app = angular.module('myApp',[]);</script>

Line 28: var app = angular.module("myApp");

Line 34: angular.module('myApp').controller(...

fiddle: http://jsfiddle.net/uXpqL/

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

4 Comments

I changed the code to use app instead of angular.module(), didn't change a thing
Just to check something, but when you have in your question: <script src="js/account-menu.js">function AccountMenuCtrl ($scope) {}</script> you don't ACTUALLY have a src defined and script contents as well right? That's just showing the contents of your external file?
Because aside from the multiple module declarations and the src tags, this is fine: plnkr.co/edit/5mMxvNf2dNVZ7XQoYG5F?p=info
That's just showing the contents of your external file? - Yes. Fixing the multiple modules and another function did it here. It finally works :-) If anyone is curious: I used var as a function parameter, not knowing that var is a reserved word.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.