user3125693
user3125693

Reputation: 950

Accessing angularjs service properties within service

I have an angularjs service defined below. Within my angular service, I want to access my service variables. There are 2 locations: which I commented "FIRST THIS" and SECOND THIS". At "FIRST THIS", I can access my "svc" variable properly, so this.testvar would give me 'foo1'. However, at "SECOND THIS", the "this" variable changes, and is no longer the "svc" variable. How can I access the svc variable at "SECOND THIS"?

Also a side question... is there a name for the type of variable that svc is? It's not a JSON because it has a function as one of the key values.

    angular
      .module('myApp')
      .service('myService', function($q, $http, $cookies, $state) {
          var svc = {
            testvar: 'foo1',
            func1: function(callback) {

              console.log(this); // FIRST THIS
              $http.post(endpoint).success(function(response) {
                console.log(this); // SECOND THIS
              }).error(function(response, status) {

              });
            },
          }
          return svc
        }

Upvotes: 2

Views: 315

Answers (3)

alphapilgrim
alphapilgrim

Reputation: 3975

You could do something like this, hope it helps.Did some restructuring.

angular
  .module('myApp')
  .service('myService', function($q, $http, $cookies, $state) {
      var service = this;
      var testvar = 'foo1';

      function func1(callback) {
        console.log(service); // FIRST THIS
        return $http
          .post(endpoint)
          .success(function(response) {
            console.log(service); // SECOND THIS
          })
          .error(function(response, status) {

          });
      }

      function example() {

      }

      function exampleOne() {

      }

      return {
        func1: func1,
        example: example,
        exampleOne: exampleOne
      };
    };

Upvotes: 0

Matthew Cawley
Matthew Cawley

Reputation: 2818

Declare a new variable e.g. self to use in place of this, then it will mean the same thing throughout the service.

angular.module('myApp').service('myService', function($q, $http, $cookies, $state) {

    var self = this; // Declare a new variable to act as a handle to "this";

    var svc =  {
        testvar: 'foo1',
        func1: function(callback) {

            // Use the new variable here.
            console.log(self);

            $http.post(endpoint).success( function(response) {

                // Use the new variable here also.
                console.log(self);

            }).error(function(response, status){

            });
        },
    }
    return svc
}

svc is an Object

Upvotes: 1

Brunno
Brunno

Reputation: 977

In the "SECOND THIS" the scope(this) belongs to $http scope instead of "FIRST THIS". You can declare other variable _that to save the last scope, for example:

func1: function(callback) {

  var _that = this;

  $http.post(endpoint).success(function(response) {
    //use _that here :)
  }).error(function(response, status) {

  });
}

Upvotes: 0

Related Questions