How to use multiple ng-app and ng-view in one angularjs project (not single page application)?

Hi world!
is it possible to use multiple ng-app and ng-view in the same project that’s composed by multiple pages?

Looking at the Angular docs for ng-app, it says this…

only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap instead.

That does suggest you can have more than one Angular app on a site, though I’m not sure how you’d set that up in a single project. What is it you’re trying to achieve exactly.

I want to use two ng-app but not in the same HTML document, each ng-app for a document, then use ng-view in each HTML document to show other templates, it doesn’t work for me :confused:

I’m a little confused by your description. It sounds like you have two separate Angular apps here (i.e. they load from separate HTML pages). I’m not sure how the two are related, though?

Hi guys I’m newbie here. Just want to ask if it is possible to use multiple ng-app?

In the same project i have multiple pages , and i want to use ng-apps in those pages as i need, but not multiple ng-app in the same html page.

So you’ve got something like this:

page1.html

...
<body ng-app="app1">
...

page2.html

...
<body ng-app="app2">
...

and you want to know how to combine the code for both apps into the same project (so they can share code, presumably)? Have I understood you correctly?

1 Like

Yeah! and actually i was confused if i could do what you’re saying :slight_smile:

I recommend having a read of the module documentation (check especially the code example under the ‘Recommended Setup’ section), as what you need to do is move any shared code (controllers, services, directives etc.) into separate modules which you can then list as dependencies of each app that needs them.

As long as you include your script(s) with your shared modules in the page before your main application script, everything should work fine.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.