0

I have a task to integrate RequireJS to existing large ASP.NET application that already has AngularJS code.

The page already bootstraps AngularJS with ng-app='xxx' in master page. I'm trying to create a new controller that will loaded by RequireJS. In HTML I'm defining new div as following

<div ng-controller='someConroller'></div>

someConroller.js loaded by RequireJS after AngularJS has bootsraped, thus I got the following error:

"Argument 'someConroller' is not a function, got undefined"

The problem as I understand is that AngularJS has bootstraped, HTML is loaded, but 'someController' is still doesn't loaded.

How can I cause to AngularJS wait for controller will finish his loading?

Please note that I can't integrate routing.

Please, advise.

1 Answer 1

1

You need to bootstrap your project only when someContoller is loaded, so do this:

require(['someController'], function(){
    angular.bootstrap(document, ['xxx']);
});

in your main file and remove ng-app='xxx' in master page. What this does, it requires someController to be loaded before bootstraping you project.

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

3 Comments

Since there is a lot of legacy code that includes other AngularJS code, I can't bootstrap AngularJS when someController is loaded.
AngularJS works with what's already loaded, it can't instantiate controller if there is none loaded. Since you are depending on someController loading, that's the only solution I can think of.. That's what requirejs is for - managing dependencies, providing modular entities and so on. In this case your dependency is someController so any code execution can be done only when this is loaded.
I would suggest either doing everything the old way, or everything in requirejs way. Using those two approaches together doesn't seem to work on my experience.

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.