[javascript] AngularJS: How can I pass variables between controllers?

The following example shows how to pass variables between siblings controllers and take an action when the value changes.

Use case example: you have a filter in a sidebar that changes the content of another view.

_x000D_
_x000D_
angular.module('myApp', [])_x000D_
_x000D_
  .factory('MyService', function() {_x000D_
_x000D_
    // private_x000D_
    var value = 0;_x000D_
_x000D_
    // public_x000D_
    return {_x000D_
      _x000D_
      getValue: function() {_x000D_
        return value;_x000D_
      },_x000D_
      _x000D_
      setValue: function(val) {_x000D_
        value = val;_x000D_
      }_x000D_
      _x000D_
    };_x000D_
  })_x000D_
  _x000D_
  .controller('Ctrl1', function($scope, $rootScope, MyService) {_x000D_
_x000D_
    $scope.update = function() {_x000D_
      MyService.setValue($scope.value);_x000D_
      $rootScope.$broadcast('increment-value-event');_x000D_
    };_x000D_
  })_x000D_
  _x000D_
  .controller('Ctrl2', function($scope, MyService) {_x000D_
_x000D_
    $scope.value = MyService.getValue();_x000D_
_x000D_
    $scope.$on('increment-value-event', function() {    _x000D_
      $scope.value = MyService.getValue();_x000D_
    });_x000D_
  });
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>_x000D_
_x000D_
<div ng-app="myApp">_x000D_
  _x000D_
  <h3>Controller 1 Scope</h3>_x000D_
  <div ng-controller="Ctrl1">_x000D_
    <input type="text" ng-model="value"/>_x000D_
    <button ng-click="update()">Update</button>_x000D_
  </div>_x000D_
  _x000D_
  <hr>_x000D_
  _x000D_
  <h3>Controller 2 Scope</h3>_x000D_
  <div ng-controller="Ctrl2">_x000D_
    Value: {{ value }}_x000D_
  </div>  _x000D_
_x000D_
</div>
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to angularjs

AngularJs directive not updating another directive's scope ERROR in Cannot find module 'node-sass' CORS: credentials mode is 'include' CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response WebSocket connection failed: Error during WebSocket handshake: Unexpected response code: 400 Print Html template in Angular 2 (ng-print in Angular 2) $http.get(...).success is not a function Angular 1.6.0: "Possibly unhandled rejection" error Find object by its property in array of objects with AngularJS way Error: Cannot invoke an expression whose type lacks a call signature

Examples related to angularjs-controller

Check if value exists in the array (AngularJS) How do I inject a controller into another controller in AngularJS Error: [ng:areq] from angular controller How do I use $rootScope in Angular to store variables? AngularJS : The correct way of binding to a service properties Using $setValidity inside a Controller AngularJS: How can I pass variables between controllers?