Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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