I'm trying to inject angular's $timeout
service in a method that should be called in several controllers in my application. However, I'm always getting the error:
Error: [$injector:unpr] Unknown provider: $timeoutProvider <- >$timeout
Of course $timeout
has to be known since it's an angular service, so I don't see why this error is happening.
This is my code:
HTML:
<div ng-app="app" ng-controller="sampleController">
<button ng-click="doit()">Do It!</button>
</div>
JAVASCRIPT:
var app = angular.module('app', []);
app.controller('sampleController', ['$scope', function($scope) {
var _this = this;
$scope.doit = function() {
var $injector = angular.injector();
var $timeout = $injector.get('$timeout', _this);
$timeout(function () { alert('ok'); });
};
}]);
And here's a jsfiddle demonstrating the problem:
https://jsfiddle.net/fvw8zss5/
- This is not a question about if that's a bad practice or not. Of course it is bad. It's a question about why it was not working.
- The JsFiddle will work if you know how to use JsFiddle.