0

Here is my code :

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<script type="text/javascript" src="./angular.min.js"></script>
<script>
    var app = angular.module('myApp', []);
    var app1 = angular.module('myApp1', []);
    app.controller('myCtrl', function($scope) {
        $scope.firstName = "sparsh";
        $scope.lastName = "khandelwal";
    });
</script>
<title>Home Page</title>
</head>
<body>
    <div ng-app="myApp" ng-init="name='Sparsh'">
        <div ng-controller="myCtrl">{{firstName}}</div>
        <p>
            Name : <input type="text" ng-model="name">
        </p>
        <h1>Hello {{name}}</h1>

    </div>
    <div ng-app="myApp1" ng-init="names=['Jani','Hege','Kai']">
        <ul>
            <li ng-repeat="x in names">{{x}}</li>
        </ul>
    </div>
    <script type="text/javascript">
        angular.bootstrap(document, ['myApp1','myApp']);
    </script>
</body>
</html>

Although i can see the expected output, but at the same time facing console error.

enter image description here

here is the description of error

(while i click on the url)

enter image description here

it says it already bootstrapped so i remove the 'myApp' from .bootstrap function, but that didnt work.

Please help.

5
  • jsfiddle.net/arunpjohny/q13j4weL/2 Commented Apr 25, 2015 at 12:38
  • @ArunPJohny Code is working , but giving error in console. Commented Apr 25, 2015 at 12:41
  • The fiddle is not giving any error for me - jsfiddle.net/arunpjohny/q13j4weL/3 - after removing both ng-app attributes from the html Commented Apr 25, 2015 at 12:42
  • @ArunPJohny if you remove them , how can you say which div is associated with which app ? Commented Apr 25, 2015 at 12:49
  • try this approach - jsfiddle.net/arunpjohny/q13j4weL/5 Commented Apr 25, 2015 at 13:13

2 Answers 2

1

You are doing both ng-app declarations and angular.bootstrap(document, ['myApp1','myApp']);.

You need to choose only one method, otherwise you get the well descripted error of multiple bootstrapped applications.

Sign up to request clarification or add additional context in comments.

5 Comments

@SparshKhandelwal that is in the case if there are multiple ng-app attributes... here you are using auto initialization and manual initialization which is causing the multiple initialization error
@SparshKhandelwal You might want to do 2 bootstrapping according to element IDs, like this: jsfiddle.net/seyenaz/q13j4weL/4
@OmriAharon please elaborate it , i am not able to understand : 'here you are using auto initialization and manual initialization which is causing the multiple initialization error'
You need to either declare ng-app="myApp" or angular.bootstrap(..), not both. These are 2 ways to bootstrap, you have to choose one.
0

The problem is as explained in the previous comments, the automatic and manual initialization of modules.

Since you want to initialize them separately for each elements then try an approach like

<div data-app="myApp" ng-init="name='Sparsh'">
    <div ng-controller="myCtrl">{{firstName}}</div>
    <p>
        Name : <input type="text" ng-model="name"/>
    </p>
    <h1>Hello {{name}}</h1>

</div>
<div data-app="myApp1" ng-init="names=['Jani','Hege','Kai']">
    <ul>
        <li ng-repeat="x in names">{{x}}</li>
    </ul>
</div>

then

var app = angular.module('myApp', []);
var app1 = angular.module('myApp1', []);
app.controller('myCtrl', function ($scope) {
    $scope.firstName = "sparsh";
    $scope.lastName = "khandelwal";
});

var els = document.querySelectorAll('[data-app]');
for (var i = 0; i < els.length; i++) {
    angular.bootstrap(els[i], [els[i].dataset.app]);
}

Demo: Fiddle

3 Comments

@SparshKhandelwal ar you sure it is the same as the fiddle.. because there is no error in the fiddle.. can you post the error message... did you rename ng-app to data-app
i missed that :) now working, but what is difference between data-app and ng-app.? and please elaborate this : 'the automatic and manual initialization of modules '
change it from ng-app to data-app is like removing ng-app .....instead of data-app .. write anything or simply remove it ... everything will work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.