2

Can i use one service into another angular service? Else what will be the best approach to accomplish the below?

First Service:

var app = angular.module("MyApp", []);
app.service('Service1', function () {
    return {
        FirstFunction: function () {
            return "something";
        }
    }
});

Second service:

app.service('Service2', function () {
    return {
        SecondFunction: function () {
           //How to use FirstFunction of Service1 ???
        }
    }
});

Thanks

2 Answers 2

3
app.service('Service2', function (Service1) {
    return {
        SecondFunction: function () {
           Service1.FistFunction
        }
    }
});

As long as service2 doesnt depend on service1 there is no problem.

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

Comments

2
app.service('Service2', function (Service1) {
    return {
        SecondFunction: function () {
           Service1.FirstFunction();
        }
    }
});

1 Comment

Thanks alot.Works great. I just tried the below which didnt work. What is the reason. app.service('Service2',["Service1", function (Service1) { return { SecondFunction: function () { Service1.FirstFunction(); } } }]);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.