I have just started learning Angular JS and created some basic samples however I am stuck with the following problem.
I have created 2 modules and 2 controllers.
shoppingCart -> ShoppingCartController
namesList -> NamesController
There are associated views for each controller. The first View renders fine but second is not rendering. There are no errors.
Please help me solve this issue.
Also is there any possibility to add console in View to check what values are passed from Controller.
e.g. in the following div can we add console.log and output the controller values
<div ng-app="shoppingCart" ng-controller="ShoppingCartController">
</div>
To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap()
HTML
JS
The reason for this is that only one AngularJS application can be automatically bootstrapped per HTML document. The first
ng-app
found in the document will be used to define the root element to auto-bootstrap as an application.In other words, while it is technically possible to have several applications per page, only one ng-app directive will be automatically instantiated and initialized by the Angular framework.
You can define a Root ng-App and in this ng-App you can define multiple nd-Controler. Like this
Updated JsFiddle:
http://jsfiddle.net/ep2sQ/1011/You can use
angular.bootstrap()
directly... the problem is you lose the benefits of directives.First you need to get a reference to the HTML element in order to bootstrap it, which means your code is now coupled to your HTML.
Secondly the association between the two is not as apparent. With
ngApp
you can clearly see what HTML is associated with what module and you know where to look for that information. Butangular.bootstrap()
could be invoked from anywhere in your code.If you are going to do it at all the best way would be by using a directive. Which is what I did. It's called
ngModule
. Here is what your code would look like using it:You can get the source code for it at:
http://www.simplygoodcode.com/2014/04/angularjs-getting-around-ngapp-limitations-with-ngmodule/
It's implemented in the same way as
ngApp
. It simply callsangular.bootstrap()
behind the scenes.