javascript - multiple apps in single page in angular js - Stack Overflow

I am buliding an application in angular js.I have two controllers for two views.javascript:var myApp1 =

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

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

Share Improve this question edited Jan 2, 2014 at 9:51 user67867 asked Jan 2, 2014 at 6:51 user67867user67867 4214 gold badges11 silver badges24 bronze badges 3
  • you can only have one module on a single page. – Ravi Commented Jan 2, 2014 at 6:53
  • 1 I think myApp1.controller('myController2'... should be myApp2.controller('myController2'... – Joonas Commented Jan 2, 2014 at 6:55
  • corrected.but same issue persists – user67867 Commented Jan 2, 2014 at 7:06
Add a ment  | 

2 Answers 2

Reset to default 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

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745189265a4615785.html

相关推荐

  • javascript - multiple apps in single page in angular js - Stack Overflow

    I am buliding an application in angular js.I have two controllers for two views.javascript:var myApp1 =

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信