0

I want to seperate my WebApp in different modules for directives, services, controllers etc. just like angular-seed. Somehow the controllers defined in controllers.js don't seem to work.

<ul class="menu" ng-controller="MenuController">
    <li>{{hello}}</li>
</ul>

In app.js I defined the global App module with it's submodules.

// Declare app level module which depends on filters, and services
angular.module('myApp', [
    'myApp.filters',
    'myApp.services',
    'myApp.directives',
    'myApp.controllers'
]);

And in controllers.js I tried to define a controller, that does not load. When the page get rendered I just see {{hello}}.

angular.module('myApp.controllers', []).
  controller('MenuController', function($scope) {
     $scope.hello = "world"; 
  });

How do I attach the controllers to their template counterparts?

6
  • Did you reference the controller script in your index.html? Commented Dec 25, 2013 at 14:17
  • @stefchri Yes, I did. I'm sure it's loaded (Angular aswell). Because when I create the controller globally in controllers.js: function MenuController($scope){ $scope.hello = "world"; } it works.. Commented Dec 25, 2013 at 14:19
  • 1
    Ensure that controller.js is loaded before app.js Commented Dec 25, 2013 at 14:22
  • @KhanhTO Thanks, that did the trick. If you'd make it an answers, I'd accept it. Commented Dec 25, 2013 at 14:26
  • 1
    Please do, otherwise the question will remain unanswered. Commented Dec 25, 2013 at 14:42

2 Answers 2

1

The problem is because the other modules aren't defined. Modifying the myApp module like below works:

angular.module('myApp', [

    'myApp.controllers'
]);

http://jsbin.com/osOQOYag/2/edit?html,js,output

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

Comments

0

It seems like angular-seed has the following order of scripts in their /app/index.html:

<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-route.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
<script src="js/filters.js"></script>
<script src="js/directives.js"></script>

As @KhanhTO pointed out in the comments, app.js should be called last.

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.