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>
5
  • 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. Commented Jul 17, 2015 at 9:01
  • If I call the function fun1 then I can get the ctrl1 values, but ctrl2 values? Commented Jul 17, 2015 at 9:04
  • in that case ctrl2 value would be undefiend.. check this. If you need both values to be set before accessing then just check if either of the value is null or not. Commented Jul 17, 2015 at 9:07
  • But I need both controller values, what can I do? Commented Jul 17, 2015 at 9:10
  • thats what I said, add a check before you use the values. Commented Jul 17, 2015 at 9:15

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

then how can I access the different controller values from same place?
You can return objects from a factory.
@WédneyYuri can you give me a simple example?
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.

3 Comments

Storing a specific $scope in a service object looks like a huge memory leak in my view ... +1 for the "rethink your approach" though
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.
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 ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.