Multiple ngApp in 1 page or 1 application

I encounter this issue where there are 2 applications which is totally handled by different ngApp but they want to put them as a same application. After a quick search online, found that actually we can bootstrap the page with different ngApp manually instead of by defining ngApp at body tag. See below sample source for example

HTML

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>AngularJS Plunker</title>
        <script>document.write('<base href="' + document.location + '" />');</script>
        <link rel="stylesheet" href="style.css">
    
    </head>
    <body ng-app="HelloWorldApp">
        Test 1
        <section id="helloworld" ng-controller="HelloWorldController">
            Hello {{name}}!
        </section> 
        <br />
        <section id="myapp" ng-controller="MyNameIsController">
            My Name is {{FirstName}} {{LastName}}!
        </section>
        <script src="bower_components/angular/angular.js" type="text/javascript"></script>
        <script src="app.js" type="text/javascript"></script>
        <script type="text/javascript">
            // angular.bootstrap(document.getElementById('helloworld'), ['HelloWorldApp']);
            angular.bootstrap(document.getElementById('myapp'), ['MyNameIsApp']);
        </script>
    </body>
</html>

Angular JS

var HelloWorldApp = angular.module('HelloWorldApp', []);

HelloWorldApp.controller('HelloWorldController', function($scope) {
    $scope.name = 'World';
});

var MyNameIsApp = angular.module('MyNameIsApp', []);

MyNameIsApp.controller('MyNameIsController', function($scope) {
    $scope.FirstName = 'Testing';
    $scope.LastName = 'manual bootstrapiing';
});


Summary
So basically, the main point here is 
angular.bootstrap(document.getElementById('myapp'), ['MyNameIsApp']); 

This is how we manually bootstrap the page with other ngApp other than the one defining on the body tag.
 

Comments

Popular posts from this blog

Django Form: MultipleChoiceField and How To Have Choices From Model Example

DJango Queryset comparing date time

Jquery Validation fix for date format dd/mm/yyyy in Chrome and Safari