Skip to content Skip to sidebar Skip to footer

Common Controller For Multiple Ng-app In AngularJS

I have a common controller that I need access in multiple ng-app scope Ex: Controller function commonCtrl($scope){ $scope.data = { message: 'Hello World' }; } First ng-app <

Solution 1:

Create a 3rd module and define the controller in that module. Refer the new module in the two ngApp module you have created. Also the controller has to be defined using the module controller method.

var sharedModule=angular.module('shared',[]);
sharedModule.controller('commonCtrl',['$scope',function($scope) {

}]);

Then use

angular.module('firstApp', ['shared']);
angular.module('secondApp', ['shared']);

Post a Comment for "Common Controller For Multiple Ng-app In AngularJS"