Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a AngularJS app, that depends on a webservice, I would like to load some more controllers into the app from a remote host, after the app is loaded. I don't know i this is possible?

In my controller, I want to load some more controllers (js files)

.controller('FrontpageCtrl', function($scope, $stateParams, $filter, $sce, contentService) {
    console.log("hitting FrontpageCtrl ... ");
    contentService.promise.then(function(data){
        var page = $filter('filter')(data, {id:$stateParams.pageId})[0];
        $scope.content = $sce.trustAsHtml(page.content);

        // Load a controller, directive and other provided by the webservice!
        $scope. ...
    });
})
share|improve this question
    
I think you have to add this files dynamically to your app template (eg. in <head> section), and then reload the whole page. –  akn Aug 20 '14 at 10:07

1 Answer 1

up vote 2 down vote accepted

Currently angular does not provide a way to load modules dynamically. Hence, any angular built in object (directives, controllers, factories, etc.).
This means your controllers (from the web service) should be loaded on bootsrapping angular (probably as a resource on the index page).
There are some ways to dynamically load stuff after bootstrapping, here are a few:

  1. My personal favorite: https://github.com/ocombe/ocLazyLoad.
  2. Closest to your question: http://weblogs.asp.net/dwahlin/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs
  3. https://www.startersquad.com/blog/angularjs-requirejs/

There are many more stuff to be found.. you can obviously google it.

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.