I'm trying to add two angular apps / modules to one page. In the fiddles below you can see that always only the first module, referenced in the html code, will work correctly, whereas the second is not recognized by angular.
In this fiddle we can only execute the doSearch2
method, whereas in this fiddle only the doSearch
method works correctly.
I'm looking for the way how to correctly place two angular modules into one page.
See also
I created an alternative directive that doesn't have
ngApp
's limitations. It's calledngModule
. This is what you code would look like when you use it:You can get the source code at:
http://www.simplygoodcode.com/2014/04/angularjs-getting-around-ngapp-limitations-with-ngmodule/
It's essentially the same code used internally by AngularJS without the limitations.
Manual bootstrapping both the modules will work. Look at this
Here is the link to the Plunker http://plnkr.co/edit/1SdZ4QpPfuHtdBjTKJIu?p=preview
NOTE: In html, there is no
ng-app
.id
has been used instead.I made a POC for an Angular application using multiple modules and router-outlets to nest sub apps in a single page app. You can get the source code at: https://github.com/AhmedBahet/ng-sub-apps
Hope this will help
This might be not straight forward answer. But while bootstrap ng-app please take care of the following thing.
Do not bootstrap the app on an element with a directive that uses transclusion, such as ngIf, ngInclude and ngView. Doing this misplaces the app $rootElement and the app's injector, causing animations to stop working and making the injector inaccessible from outside the app.
For more information on angular.bootstrap please look into the documentation.
Why do you want to use multiple [ng-app] ? Since Angular is resumed by using modules, you can use an app that use multiple dependencies.
Javascript:
HTML:
EDIT
Keep in mind that if you want to use controller inside controller you have to use the controllerAs syntax, like so: