1

I am using angular 1.x and I am trying to share data from one controller to another

I am using the above model in mainctrl. The radiotmplt.radiohead=='IRU600v3'is from firstctrl. I cannot share data using rootscope. Please advise.

vpdeva
  • 303
  • 2
  • 11

1 Answers1

1

Here is the demo how to share data using RootScope

link Jsfiddle

Js

  var app = angular.module('myApp', []);

  app.controller('ctrl1', function($scope, $rootScope) {
    $scope.data = 'data';
    $rootScope.data1 = 'old data';
    $scope.setVal = function() {
      $rootScope.data1 = 'new data';
    }
  });

  app.controller('ctrl2', function($scope, $rootScope) {
    $scope.data = $rootScope.data1;
    $scope.$watch('data1', function(o, n) {

      $scope.data = $rootScope.data1;

    })
  });

HTML

  <div ng-app='myApp'>
    <div ng-controller='ctrl1'>
      controller 1
      <input type='text' ng-model='data'>
      <button ng-click='setVal()'>
        Change
      </button>
    </div>
    <hr>
    <div ng-controller='ctrl2'>
      controller 2
      <input type='text' ng-model='data'>
    </div>


  </div>

Hope this will help you

Jayant Patil
  • 1,537
  • 2
  • 11
  • 18