0

I want to access the controller values from service, how can I access it. I am try to access the controller values by using the following code. The code is here JsBin.com

    <script>

    var app = angular.module('app', [])
.controller("ctrl1",['$scope','svc',function($scope,svc){

    $scope.fun1=function(){
      svc.service_set();

      alert(svc.txt1);
      alert(svc.txt2);

    }

}])
.controller("ctrl2",['$scope','svc',function($scope,svc){

    $scope.fun2=function(){
        svc.service_set();

        alert(svc.txt1);
        alert(svc.txt2);
    }

}]).
service("svc",function(){
      var svc={};
      svc.service_set=function()
      {
        //I want to access the controller values from here
        svc.txt1=ctrl1.c1txt1; 
        svc.txt2=ctrl2.c2txt1;

      }

      return svc;
    })
;

    </script>
Merbin Joe
  • 611
  • 6
  • 27
  • Dont use `ctrl1` and `ctrl2` inside the service instead pass the value to the set function like `service_set_txt1 = function(val){svc.txt1 = val}` and same with txt2 and just access the `svc` object whereever you need. – Nilesh Jul 17 '15 at 09:01
  • If I call the function fun1 then I can get the ctrl1 values, but ctrl2 values? – Merbin Joe Jul 17 '15 at 09:04
  • in that case ctrl2 value would be undefiend.. check [this](http://jsbin.com/rezamofico/1/edit?html,output). If you need both values to be set before accessing then just check if either of the value is null or not. – Nilesh Jul 17 '15 at 09:07
  • But I need both controller values, what can I do? – Merbin Joe Jul 17 '15 at 09:10
  • thats what I said, add a check before you use the values. – Nilesh Jul 17 '15 at 09:15

2 Answers2

1

You should not use controllers inside your service. Services are meant to be used as container for reusable logic. Instead of calling controller from service, call service methods from controller

Ajay Kumar
  • 1,154
  • 2
  • 10
  • 24
0

If you want to store controller values inside your service, pass the value with a connection to the controller. Or in your case, $scope.

.service(function () {
  var dataStore = {};

  this.set = function (ctrl, value) {
    dataStore[ctrl] = value;
  });

  this.get = function (ctrl) {
    return ctrl ? dataStore[ctrl] : dataStore;
  };
});

updated your jsbin


I would rethink your approach; If there are values residing in controllers that need to be accessed by other controllers, then said values should originate from services and/or factories. Or even just plain constants/values.

The controller would then fetch the values it desires, possibly modify them, and send any modifications back to the service so as to keep in sync.

The controller is the glue between your services and the view, not the other way around.

  • Storing a specific $scope in a service object looks like a huge memory leak in my view ... +1 for the "rethink your approach" though – Jscti Jul 17 '15 at 09:53
  • Hm. I'm not so sure I would agree on the memory leak aspect, assuming that you setup a listener for the `$destroy` event and clean out any references in your service on said event. *But even then, I don't think it's the correct way to go about the presented problem.* –  Jul 17 '15 at 09:55
  • 2
    I though you wanted to store a whole $scope object in the service, but it's only the storing of a key/value. forget my remark ;) – Jscti Jul 17 '15 at 09:58