d4rty d4rty - 2 months ago 22
AngularJS Question

Using a constant inside of a service - AngularJS

I am trying to use two constants inside a service, but the constants are undefined. I also included these constants in different controllers and there isn't the problem with undefined variables. Why is this the case?

var app = angular.module("angularJsApplication", ["ngRoute"]);

app.config(function ($routeProvider) {
$routeProvider
.when("/overview", {
...
});
});


app.constant('user1Constant', "aa");
app.constant('user2Constant', "bb");


app.factory("databaseService", [function (user1Constant, user2Constant) {
console.log(user1Constant); //user1Constant is undefined

Answer

Your factory Should be like this,

app.factory("databaseService", ["user1Constant","user2Constant", function(user1Constant,user2Constant) {
console.log(user1Constant);
}