Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am using requirejs and angularjs to load the module inside my application. The problem I am facing is I dont know how to load the controller after angular has been bootstrapped. Here is my code :

main.js :

require(['app', //all my scripts go here 
function(){
 angular.bootstrap(document, ['myApp']);
});

app.js :

define([], function(){
    var myApp = angular.module('myApp', ['ngRoute']);
    $routeProvider.
        when('/home', {
                templateUrl : 'partials/home.html',
                controller : 'UserRegisterCtrl'
            }).
            otherwise({
                redirectTo : '/home'
            })
    return myApp;
})

controller.js

define(['app'], function(app){
    app.controller('MyCtrl', function($scope){});
})

myApp.html

body(ng-app)
     <div ng-controller = 'MyCtrl'></div>  <-- I can not put MyCtrl here 
because I think the MyCtrl has been declared before myApp has been bootstrapped

Therefore I really need to know if there is a way to load the MyCtrl controller after myApp has been bootstrapped. I am really new to Angularjs. Any help would be really appreciate.

share|improve this question
    
A complex problem. Take a look at a couple of efforts, there are even more in the wild... none is complete I think: angular-require-lazy, angularAMD – Nikos Paraskevopoulos Jan 10 '14 at 8:24
up vote 1 down vote accepted

If you are manually bootstrapping your angular app there is no need for ng-app, because this is just a directive that pretty much does the same thing.

This little trap caused some people a headache before:

share|improve this answer

When you use ng-app in your html, the bootstrap happens automatically. In this case, it depends what you put in the <script> tag in your index.html.

I am assuming that you follow the RequireJS usage where you load angular using RequireJS. If so, make sure you declare the dependency of angular to your app using shim. If that is not the case, please state how your are loading angular and share the content of your main.js.

It was really tricky for me to get RequireJS to work with AngularJS so I created following project that helped me deal with the complexity:

http://marcoslin.github.io/angularAMD

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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