Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one AngularJS controller call another?

Is it possible to have one controller use another?

For example:

This HTML document simply prints a message delivered by the MessageCtrl controller in the messageCtrl.js file.

<html xmlns:ng="http://angularjs.org/"> <head>     <meta charset="utf-8" />     <title>Inter Controller Communication</title> </head> <body>     <div ng:controller="MessageCtrl">         <p>{{message}}</p>     </div>      <!-- Angular Scripts -->     <script src="http://code.angularjs.org/angular-0.9.19.js" ng:autobind></script>     <script src="js/messageCtrl.js" type="text/javascript"></script> </body> </html> 

The controller file contains the following code:

function MessageCtrl() {     this.message = function() {          return "The current date is: " + new Date().toString();      }; } 

Which simply prints the current date;

If I were to add another controller, DateCtrl which handed the date in a specific format back to MessageCtrl, how would one go about doing this? The DI framework seems to be concerned with XmlHttpRequests and accessing services.

like image 426
BanksySan Avatar asked Feb 15 '12 12:02

BanksySan


People also ask

Can we call one controller from another controller in AngularJS?

In AngularJS when it comes to communicating between controllers, one would naturally assume to reference another controller you can simply inject it into another controller and call its methods: however, you cannot do that.

Can we have two controllers in AngularJS?

Angular creates one $scope object for each controller. We also have a $rootScope accessible from every controllers.In case of multiple controllers AngularJS framework creates and pass a different $scope object to each controller so that data and methods of one controller not be accessed in another controller.


2 Answers

There are multiple ways how to communicate between controllers.

The best one is probably sharing a service:

function FirstController(someDataService)  {   // use the data service, bind to template...   // or call methods on someDataService to send a request to server }  function SecondController(someDataService)  {   // has a reference to the same instance of the service   // so if the service updates state for example, this controller knows about it } 

Another way is emitting an event on scope:

function FirstController($scope)  {   $scope.$on('someEvent', function(event, args) {});   // another controller or even directive }  function SecondController($scope)  {   $scope.$emit('someEvent', args); } 

In both cases, you can communicate with any directive as well.

like image 63
Vojta Avatar answered Oct 16 '22 01:10

Vojta


See this fiddle: http://jsfiddle.net/simpulton/XqDxG/

Also watch the following video: Communicating Between Controllers

Html:

<div ng-controller="ControllerZero">   <input ng-model="message" >   <button ng-click="handleClick(message);">LOG</button> </div>  <div ng-controller="ControllerOne">   <input ng-model="message" > </div>  <div ng-controller="ControllerTwo">   <input ng-model="message" > </div> 

javascript:

var myModule = angular.module('myModule', []); myModule.factory('mySharedService', function($rootScope) {   var sharedService = {};    sharedService.message = '';    sharedService.prepForBroadcast = function(msg) {     this.message = msg;     this.broadcastItem();   };    sharedService.broadcastItem = function() {     $rootScope.$broadcast('handleBroadcast');   };    return sharedService; });  function ControllerZero($scope, sharedService) {   $scope.handleClick = function(msg) {     sharedService.prepForBroadcast(msg);   };    $scope.$on('handleBroadcast', function() {     $scope.message = sharedService.message;   });         }  function ControllerOne($scope, sharedService) {   $scope.$on('handleBroadcast', function() {     $scope.message = 'ONE: ' + sharedService.message;   });         }  function ControllerTwo($scope, sharedService) {   $scope.$on('handleBroadcast', function() {     $scope.message = 'TWO: ' + sharedService.message;   }); }  ControllerZero.$inject = ['$scope', 'mySharedService'];          ControllerOne.$inject = ['$scope', 'mySharedService'];  ControllerTwo.$inject = ['$scope', 'mySharedService']; 
like image 32
adardesign Avatar answered Oct 16 '22 01:10

adardesign