I want to know is how to use multiple controllers for a single page application. I have tried to figure it out and I've found questions very similar to mine, but there is just a ton of different answers solving a specific problem where you end up not using multiple controllers for a single page app.
Is that because it would not be wise to use multiple controllers for a single page? Or is it just not possible?
Let's say I already have a kick-ass image carousel controller working the main page, but then I learn how to (let's say) use modals and I need a new controller for that as well (or any other thing I need a controller). What will I do then?
I have seen some answers to other questions where they ask about almost the same things as me and people answer "*OMG. Why would you even do that, just do this...".
What is the best way, or how do you do it?
Edit
Many of you are answering to just declare two controllers and then use ng-controller to call it. I use this bit of code below and then call MainCtrl with ng-controller.
app.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: "templates/main.html",
controller:'MainCtrl',
})
.otherwise({
template: 'does not exists'
});
});
Why do I even need to set a controller here if I can just use ng-controller without it? This is what confused me. (and you can't add two controllers this way, I think...)
I think you are missing the "single page app" meaning.
That doesn't mean you will physically have one .html, instead you will have one main
index.html
and several NESTED .html file. So why single page app? Because this way you do not load pages the standard way (i.e. browser call that completely refreshes the full page) but you just load the content part using Angular/Ajax. Since you do not see the flickering between page changes, you have the impression that you didn't move from the page. Thus, you feel like you are staying on a single page.Now, I'm assuming that you want to have MULTIPLE contents for your SINGLE PAGE app: (e.g.) home, contacts, portfolio and store. Your single page/multiple content app (the angular way) will be organized this way:
index.html
: contains header,<ng-view>
and footercontacts.html
:contains the contact form (no header, no footer)portfolio.html
:contains the portfolio data (no header no footer)store.html
: contains the store, no header no footer.You are in index, you click on the menu called "contacts" and what happens? Angular replaces the
<ng-view>
tag with thecontacts.html
codeHow do you achieve that? with
ngRoute
, as you are doing, you will have something like:This will call the right html passing the right controller to it (please note: do not specify
ng-controller
directive incontacts.html
if you are using routes)Then, of course, you can declare as many ng-controller directives inside your contacts.html page. Those will be child controllers of
ContactCtrl
(thus inheriting from it). But for a single route, inside therouteProvider
, you can declare a single Controller that will act as the "Father controller of the partial view".EDIT Imagine the following templates/contacts.html
the above
routeProvider
will inject a controller into your containing div. Basically the above html automaticaly becomes:When I say you can have others controllers, means that you can plug controllers to inner DOM elements, as the following:
I hope this clarify things a bit.
A
I just put one simple declaration of the app
Then I built one service and two controllers
For each controller I had a line in the JS
And in the HTML I declared the app scope in a surrounding div
and each controller scope separately in their own surrounding div (within the app div)
This worked fine
You could also have embed all of your template views into your main html file. For Example:
This way if each template requires a different controller then you can still use the angular-router. See this plunk for a working example http://plnkr.co/edit/9X0fT0Q9MlXtHVVQLhgr?p=preview
This way once the application is sent from the server to your client, it is completely self contained assuming that it doesn't need to make any data requests, etc.
I'm currently in the process of building a single page application. Here is what I have thus far that I believe would be answering your question. I have a base template (base.html) that has a div with the
ng-view
directive in it. This directive tells angular where to put the new content in. Note that I'm new to angularjs myself so I by no means am saying this is the best way to do it.base.html
menuController have access for menu div. And widgetController have access to both.
What is the problem? To use multiple controllers, just use multiple ngController directives:
You will need to have the controllers available in your application module, as usual.
The most basic way to do it could be as simple as declaring the controller functions like this: