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 want to be able to create AngularJS modules at runtime, so I do this:

<section class="panel portlet-item" id="crashpanel"></section>
<script type="text/javascript">
var angularApp = angular.module("Crashpanel", []);
angularApp.controller("CrashpanelCtrl", function ($scope, $http, $compile)
{
    console.log("Hello");
});
angular.bootstrap(angular.element("#crashpanel"), [ "Crashpanel" ]);
</script>

But "Hello" is not displayed, because the controller is not attached to the element. How to be able to attach the controller to the element ?

share|improve this question
    
you want to attach your controller to your section at runtime? Do you really need this? –  Rodrigo Fonseca Feb 10 at 14:42
    
@Rodrigo Yes, else I get the following error : docs.angularjs.org/error/… –  Julien Fouilhé Feb 10 at 14:50
    
put ng-app="Crashpanel" –  Rodrigo Fonseca Feb 10 at 14:56
    
@Rodrigo Still get the error, though everything I do afterwards works on Google chrome but still doesn't work on Firefox and that's the bug I'm trying to resolve. –  Julien Fouilhé Feb 10 at 14:58
    
do you really need to use this: angular.bootstrap(angular.element("#crashpanel"), [ "Crashpanel" ]);?? –  Rodrigo Fonseca Feb 10 at 15:02

2 Answers 2

up vote 1 down vote accepted

You need to put the data-ng-app="Crashpanel" somewhere in your code to make a reference to your module if you want, example:

<html data-ng-app="Crashpanel">

Your section must have a ng-controller attribute(like the another user said) to make a reference to your controller.

<section class="panel portlet-item" id="crashpanel" data-ng-controller="CrashpanelCtrl"></section>
share|improve this answer
    
Thanks! It works, but I don't understand why... What is the difference between having the ng-app on the html tag and having it on the section tag ? –  Julien Fouilhé Feb 10 at 15:12
    
this directive auto-bootstrap your application, i think(i don't have sure) that after angularjs in compile phase find this directive, it will only look for any other directives below(children) of this html element, not the element itself. –  Rodrigo Fonseca Feb 10 at 15:30

Change your element to this:

<section class="panel portlet-item" id="crashpanel" data-ng-controller="CrashpanelCtrl"></section>
share|improve this answer
    
I get this error docs.angularjs.org/error/… –  Julien Fouilhé Feb 10 at 14:49

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.