dk123
dk123

Reputation: 19700

AngularJs - how do functions within a service access each other?

I have the following javascript:

var myApp = angular.module('myApp', []);

myApp.factory( "TestService", function() {
    return {
        clickString: "click me",   
        clickStringProxy: this.clickString
    }
});

function MyCtrl($scope, TestService) {
    $scope.clickString = TestService.clickString;
    $scope.clickStringProxy = TestService.clickStringProxy;
}

The above is my failed attempt in trying to make TestService.clickStringProxy access TestService.clickString (through an elementary 'this').

JsFiddle: http://jsfiddle.net/eDb2S/96/

How would I go about allowing functions within a service to access each other?

I've tried various atempts to remedy the situation but to no avail.

Upvotes: 0

Views: 145

Answers (1)

shaunhusain
shaunhusain

Reputation: 19748

Declare the service object manipulate it and return.

myApp.factory( "TestService", function() {
    var service=  {
        clickString: "click me"
    }
    service.clickStringProxy = service.clickString;
    return service;
});

http://jsfiddle.net/eDb2S/97/

Upvotes: 1

Related Questions