Friday, April 21, 2017

How to pass the data from one Controller to another Controller OR one Directive to another Directive in angularjs


var app = angular.module('myApp', []);
 //Controller-1:
app.controller('FirstCtrl', function($rootScope, $scope){
     $scope.register = function(){
    $rootScope.$broadcast('myName', $scope.name)
    }
});
//Controller-2:
app.controller('SecondCtrl', function($scope){
      $scope.$on('myName', function(events, args){
        console.log(args);
        $scope.name = args;
      })
   });