2

I am buliding an application in angular js.

I have two controllers for two views.

javascript:

var myApp1 = angular.module('myApp1', []);
myApp1.controller('myController1', function($scope) {
    alert("check");
    $scope.x = 'test';
});
var myApp2 = angular.module('myApp2', []);
myApp2.controller('myController2', function($scope) {
    alert("check1");
    $scope.x = 'test';
});

HTML:

<div ng-app="myApp1">
            <div ng-controller="myController1">
                <input type="text" ng-model="x" /> {{x}}
            </div>
</div>

<div ng-app="myApp2">
            <div ng-controller="myController2">
                <input type="text" ng-model="x" /> {{x}}
            </div>
</div>

only the first controller is getting executed.

Please advice

3
  • you can only have one module on a single page. Commented Jan 2, 2014 at 6:53
  • 1
    I think myApp1.controller('myController2'... should be myApp2.controller('myController2'... Commented Jan 2, 2014 at 6:55
  • corrected.but same issue persists Commented Jan 2, 2014 at 7:06

2 Answers 2

4

You need to bootstrap each app as follows:

var myApp1 = angular.module('myApp1', []);
myApp1.controller('myController1', function($scope) {
    alert("check");
    $scope.x = 'test';
});
  angular.bootstrap(document.getElementById('myApp1Div'),['myApp1']);


var myApp2 = angular.module('myApp2', []);
myApp2.controller('myController2', function($scope) {
    alert("check1");
    $scope.x = 'test';
});
  angular.bootstrap(document.getElementById('myApp2Div'),['myApp2']);

And HTML

<div id="myApp1Div" >
            <div ng-controller="myController1">
                <input type="text" ng-model="x" /> {{x}}
            </div>
</div>

<div id="myApp2Div" >
            <div ng-controller="myController2">
                <input type="text" ng-model="x" /> {{x}}
            </div>
</div>

EDIT JSFiddle

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

6 Comments

Sorry.Both the modules are not functioning
I edit my post. What error do you see in console? Can you set up a jsfiddle?
Yes I am editing in a jsfiddle and I dont have account right and there is proxy too
Thanks.It works fine.But doesnt work when the script is given in <head>
Your can put the script in head but the angular.js script should be load first. View the source and make sure about that.
|
4

use like this

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

myApp1.controller('myController1', function($scope) {
    alert("check");
    $scope.x = 'test';
});

myApp1.controller('myController2', function($scope) {
    alert("check1");
    $scope.x = 'test';
});

HTML

<div ng-app="myApp1">
            <div ng-controller="myController1">
                <input type="text" ng-model="x" /> {{x}}
            </div>
 <div ng-controller="myController2">
                <input type="text" ng-model="x" /> {{x}}
            </div>

</div>

4 Comments

Thanks.But I have to display the two apps in different tabs.
Any simple example as above?I couldnt understand the complete scope of bootstrap
check out the description angular.bootstrap

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.