2

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.

1
  • 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 Commented Jan 10, 2014 at 8:24

2 Answers 2

1

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:

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

Comments

1

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

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.