I have an AngularJS question that drives me crazy. I have a service that looks like this (this is an example to illustrate the problem).
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 is as follows:
var app = angular.module('test-module', []);
var ctrl = app.controller('main-controller', function($scope, ToolService) {
$scope.somethingWasClicked = function() {
ToolService.runTimeoutExample();
}
});
Here's the problem, when a button is pressed that calls $ scope.somethingWasClicked, it redirects the call to the service, and I get the error message "this.doSomething is not a function".
Why? And how do I fix this? I find it difficult to find a way for my code to work this way without adding extra logic to my controller.
Thank you in advance for your help.
source
share