I have an AngularJS question that is driving me absolutely crazy. I have a service that looks like this (this is an example to illustrate the issue)
var app = angular.module('test-module');
app.service('ToolService', function($timeout){
this.doSomething = function() {
console.log("y u no referenced as method?!?");
}
this.runTimeoutExample = function(){
$timeout(function(){
this.doSomething();
}, 1000);
}
})
My controller looks like this:
var app = angular.module('test-module', []);
var ctrl = app.controller('main-controller', function($scope, ToolService) {
$scope.somethingWasClicked = function() {
ToolService.runTimeoutExample();
}
});
Here's the issue, when the button that was clicked that calls $scope.somethingWasClicked it forwards the call to the service and I get an error saying "this.doSomething is not a function".
Why? And how do I fix this? I'm having a hard time finding a way around needing my code to run like this without adding unnecessary logic into my controller.
Thanks in advance for your help