Trying to utilize the this keyword in an AngularJS factory can be a bit tricky. When you return an object with functions as properties and attempt to call one function from another using this, it may result in errors. Upon examining the this keyword through console.log, you may notice that it refers to the scope of the calling controller instead of the current returning object.
app.module('admin').factory('login', [function() {
return {
someFunc: function() {
return 'abc'
},
anotherFunc : function(){
var msg = this.someFunc();
return 'Msg is '+ msg;
},
yetAnotherFunc : function(){
var msg = this.someFunc();
return 'Msg is '+ msg;
}
}
}]).controller(['$scope', 'login', function($scope, login){
login.someFunc(); // Works Fine
login.anotherFunc(); // Error : this.someFunc is not a function
}])