0

I want to pass value from ng-click inside button, to athor html page (controller). I have one page with 'getPersonalCtrl' controller that get my vaue from ng-click, and want to sent this value to athor controller 'viewPersonalCtrl' in a diffrent html page, but I cant get the value.

this is my getPersonalCtrl (controller):

mymedical.controller('getPersonalCtrl',['$scope','$http','$cookies','myService',function($scope,$http,$cookies,myService) {

$scope.viewCurrent = function(value){
  $scope.information = value;
   myService.setInfo($scope.information);

  ///window.location="viewDetails.html";
}

}]);

this is my viewPersonalCtrl (controller) that I want to send the data to:

mymedical.controller('viewPersonalCtrl',['$scope','$http','$cookies','myService', function($scope,$http,$cookies,myService){
    $scope.myreturnedData = myService.getInfo();
    console.log($scope.myreturnedData);
}]);

mymedical.factory('myService', function(){
  console.log("factory");
var infoObj = null;//the object to hold our data
     return {
     getInfo:function(){
      console.log("getInfo");
       console.log(infoObj);
       return infoObj;
     },
     setInfo:function(value){
      //console.log("setInfo");
      //console.log(value);
      infoObj = value;
     }
     }

});

what I need to do to fix it?

Thanks,

adi
  • 81
  • 7
  • Possible duplicate of [Share data between AngularJS controllers](http://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers) – Hadi J Apr 17 '17 at 14:02
  • http://stackoverflow.com/questions/14502006/working-with-scope-emit-and-on/36377476#36377476 – Ved Apr 17 '17 at 14:03

1 Answers1

0

Can you please try like the below code?

   mymedical.factory('dummyFactory', [function () {
        var infoObj = null;
        var myServiceFactory = {
            setObj: function (value) {
                infoObj = value;
            },
            getObj: function () {
                return infoObj;
            }
        };
        return myServiceFactory;
   }]);

in controller:

dummyFactory.setObj(null);
var val2 = dummyFactory.getObj();

val2 will be null in console.

Immanuel Kirubaharan
  • 1,094
  • 1
  • 11
  • 17
  • thanks for your answer! in the secound controller I want to get the data I did as you said var val2 = myService.getObj(); and in the first controller that set the value I did $scope.viewCurrent = function(value){ $scope.information = value; myService.setObj($scope.information); } and it still no working for me, it returns null – adi Apr 19 '17 at 09:12
  • when you say var val2=myService.getObj(); is it means that you are using service not the factory? Also FYI your myService.setObj($scope.information); wont return anything.. – Immanuel Kirubaharan Apr 19 '17 at 12:01