The controller is not reading from the factory


#1

I have a services.js file where I placed my facotry:

    .factory('dataShare',function($rootScope){

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

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

    })

And my controllers, in the controller.js file look like this:

 .controller('recommendedJobsCtrl', function($q,$scope, $ionicSideMenuDelegate,$window,$http,dataShare) {

$scope.text='hey';
 $scope.post=function(){
dataShare.sendData($scope.text);
 console.log('here')
  }   

})
.controller('jobPostCtrl', function($scope,$ionicSideMenuDelegate,dataShare) {
  $scope.text = '';
 $scope.$on('data_shared',function(){
var text =  dataShare.getData();    
$scope.text = text;
console.log(text)
       });
})

When I did console.log, i realized that the service.getData isn’t wokring, i.e the receiving controller (jobPostCtrl) isn’t getting anything.
How can i fix this?


#2

Have you included services.js file? If no then include it. If yes then check if it included prior to controller.js


#3

Hi there, when you say include, what do you mean?

I’m sorry I’m new at this. Where am i supposed to include it?


#4

In your HTML page where all js files are included using


#5

yes, it’s included. But I think what’s wrong is the second controller isn’t getting the data and Im not sure why.