Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Its quite obvious but cant seem to find an answer. I am using Angular with Asp.net MVC. I can comfortably write views/templates using Razor and link js file in it for controller. It work fine as user request a page, It get served by Asp.net MVC runtime. Problem starts when I want to do SPA. Then I need to define routes. Now my ask is to simply forward Url as is to ASP.net MVC and it can return a view with a reference to angular controller in it, but angular routing forces you to either use pre resolved controller or provide a url to resolve controller. Is there any way by which controller can be picked from the view itself as it has reference to js file having controller definition and also declared it using ng-controller. This way we would be able to use excellent convention based ASP.net MVC routing with Angular SPA and will be much simpler for developers having ASP.net MVC background. To summarize, I dont want to specify controller in route config and simply provide template url to get the view which subsequently has the reference to controller ,hence controller should be resolved from there. I would be seriously disappointed if its not possible

share|improve this question
    
You dont have to pass controller parameter in route, and can use ng-controller declaration on the view. But keep in mind that all the controllers have to be loaded beforehand at the start of app bootstrapping. On demand loading does not work out of box in Angular. –  Chandermani Jul 30 at 10:38
    
Thanks for quick reply. What is the alternate for this since I dont want to load all the controllers in advance as its a huge application with tons of controllers. My view has reference to js file which has controller definition . How can I achieve this. –  Piyush Jul 30 at 10:41
    
You need to look at on demand module loading in Angular using requirejs or something similar. Still you cannot put your controller in view. It has to be a separate script file. Remember the disadvantage of multiple controllers that you perceive may not be the case. In production you combine and minify the script files. –  Chandermani Jul 30 at 10:45
    
Thanks. I am having controller as separate js file only and linked it to view.As you suggested , I will try to make sure that file is downloaded before it hits ng-controller. Will try and update –  Piyush Jul 30 at 11:00
    
Basically all Angular artifacts references have to be added on the index or home page where the app is being bootstrapped –  Chandermani Jul 30 at 11:06

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.