lukabers
lukabers

Reputation: 189

Angularjs services, how to change service property inside a service function?

how can i change this.messages inside myfunction2??? Thanks

 app.service('myservice', function() {
 this.messages=[];
 this.myfunction1=function(){
    //Stuff
    function myfunction2(){
        //"How to access this.messages now?
    }
 }
});

Upvotes: 1

Views: 103

Answers (1)

Satpal
Satpal

Reputation: 133403

You can declare a variable which will hold reference of this which can be used latter.

 app.service('myservice', function() {
     //Declare a varible to hold reference  
     var _this = this;
     this.messages = [];
     this.myfunction1 = function() {
         //Stuff
         function myfunction2() {
             //Use the reference variable
             console.log(_this.messages)
         }
     }
 });

Upvotes: 1

Related Questions