How to share data between controllers?

@nooreo92 Nooreo92
Sun, 10 Jul 2016 03:35:35 +0000
ionic

@nooreo92 wrote:

these are my controllers:

.controller('recommendedJobsCtrl', function($q,$scope, $ionicSideMenuDelegate,$window,$http,dataShare) {
    $scope.text='hey world';
    $scope.post=function(){
      dataShare.sendData($scope.text);
       console.log('sent: ' + $scope.text);
    }
 })
.controller('jobsCtrl', function($scope,dataShare) {
   $scope.words=dataShare.getData();
})

and this is my factory:

.factory('dataShare',function($rootScope){
  var service = {};

    service.sendData = function(data){
      service=data;
      $rootScope.$broadcast('data_shared', service);
     },

    service.getData = function(){
      console.log('this.data is ' + service)
      return service;

    };
  return service;
});

when the console.log prints, service is empty. How do i fix this ?

Posts: 4

Participants: 2