Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>See this fiddle: <a href="http://jsfiddle.net/simpulton/XqDxG/">http://jsfiddle.net/simpulton/XqDxG/</a></p> <p>Also watch the following video: <a href="http://www.youtube.com/watch?v=1OALSkJGsRw">Communicating Between Controllers</a></p> <p>Html:</p> <pre><code>&lt;div ng-controller="ControllerZero"&gt; &lt;input ng-model="message" &gt; &lt;button ng-click="handleClick(message);"&gt;LOG&lt;/button&gt; &lt;/div&gt; &lt;div ng-controller="ControllerOne"&gt; &lt;input ng-model="message" &gt; &lt;/div&gt; &lt;div ng-controller="ControllerTwo"&gt; &lt;input ng-model="message" &gt; &lt;/div&gt; </code></pre> <p>javascript:</p> <pre><code>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']; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload