4

I want to know how can I get the value of a variable that is inside a service, I have the following code:

myModule.service('notify', ['$window', function(win) {  
       var msgs = []; // I want to read this variable
       this.message = function(msg) {
         msgs.push(msg);
         if (msgs.length == 3) {
           win.alert(msgs.join("\n"));
           msgs = [];
         }
       };
     }]);

and I want to read the msgs variable from a controller.

3
  • 1
    Just expose a getMessages() method that return msgs; Commented Sep 30, 2015 at 20:20
  • @SheraliTurdiyev, The fact that the message method is mutating the msgs array is indeed undesired and should be avoided. Yet, exposing msgs using a method precisely answers the question. Commented Sep 30, 2015 at 21:12
  • sorry, I have updated my answer Commented Sep 30, 2015 at 22:20

3 Answers 3

6

Declaring variables with var makes them local to the scope of the function. If you want to expose it you can store it on the object itself.

myModule.service('notify', ['$window', function(win) {  
   this.msgs = []; // I want to read this variable
   this.message = function(msg) {
     this.msgs.push(msg);
     if (this.msgs.length == 3) {
       win.alert(this.msgs.join("\n"));
       this.msgs = [];
     }
   };
 }]);

Then you can retrieve msgs on your service by reading

service.msgs

A better pattern would be to create a getter method to retrieve the messages.

myModule.service('notify', ['$window', function(win) {  
   var msgs = []; // I want to read this variable
   this.getMessages = function () {
     return msgs;
   };
   this.message = function(msg) {
     msgs.push(msg);
     if (msgs.length == 3) {
       win.alert(msgs.join("\n"));
       msgs = [];
     }
   };
 }]);

Then you can retrieve your messages by calling

service.getMessages();
Sign up to request clarification or add additional context in comments.

Comments

3

Services are created with new and as a result will expose whatever is attached to this. Simply add a function that returns your messages array to this.

myModule.service('notify', ['$window', function(win) {  
    var msgs = []; // I want to read this variable

    this.message = function(msg) {
        msgs.push(msg);

        if (msgs.length == 3) {
            win.alert(msgs.join("\n"));
            msgs = [];
        }
    };

    this.getMessages = function () {
        return msgs;
    }
 }]);

Comments

0

1. You can use getter for reading

myModule.service('notify', ['$window', function (win) {
    var msgs = [];// I prefer _msgs for private variables
    Object.defineProperties(this, {
        msgs: {
            get: function () {
                return msgs;
            },
            /*set: function (val) {
                msgs = val;
            },*/ //if you need setter uncomment
            enumerable: true
        }
    });
    //other codes....
}]);

Then, you can use console.log(notify.msgs); in controller

Comments

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.