javascript - How to add multiple controller in single page in AngularJS -


i new angularjs have problem code. want add multiple controller in single ng-app. execute first one. why not second one?

<!doctype html> <html ng-app="myapp"> <head>     <script src="http://ajax.googleapis.com/ajax/libs/angul /1.4.8/angular.min.js"></script> </head> <body>     <div ng-controller="cont1">         <input type="text" ng-model="fullname">         {{fullname}}     </div>     <div ng-controller="cont2">         <input type="text" ng-model="fname">         {{fname}}     </div>     <script>         var app = angular.module("myapp", []);         app.controller('cont1', function ($scope) {             $scope.fullname = "";         });         var new = angular.module('myapp', []);         new.controller('cont2', function ($scope) {             $scope.fname = "";         });     </script> </body> </html> 

because overwriting first myapp module when var new= angular.module('myapp',[]);.

your code should be:

var app = angular.module("myapp", []); app.controller('cont1', function($scope) {   $scope.fullname = ""; }); app.controller('cont2', function($scope) {   $scope.fname = ""; }); 

or

var app = angular.module("myapp", []); app.controller('cont1', function($scope) {   $scope.fullname = ""; }); angular.module("myapp").controller('cont2', function($scope) {   $scope.fname = ""; }); 

the second parameter[] passed module() makes difference


Comments