Reputation: 235
Having a terrible time getting $timeout to work in an Angular directive. Essentially even the most simplistic implementation continues to throw a TypeError: number is not a function error.
I've done a bit of research and for the life of me can't figure out what is going wrong.
$timeout(function(){
console.log('$timeout called');
}, 500);
I'm only left to assume $timeout expects a function for the delay which makes no sense unless I am reading the documentation incorrectly.
https://docs.angularjs.org/api/ng/service/$timeout
Thanks in advance.
Stripped out everything but the $timeout implementation from the directive.
(function (window, angular, undefined) {
'use strict';
angular.module('bento.numberinput', ['bento.services'])
.factory('$helper',[
function(){
// code here
}
])
.directive('bentoNumberInput', [
'$helper',
function ($helper) {
return {
restrict : 'A',
require : '?ngModel',
replace : false,
transclude: true,
link: function($scope, $element, $attrs, $controller, $timeout){
$timeout(function(){
console.log('$timeout called');
}, 500);
}
};
}]);
})(window, window.angular);
Upvotes: 2
Views: 391
Reputation: 11752
$timeout needs to be injected at the top, much like your $helper, by the directive name and into the function as well since you're using the array form
.directive('bentoNumberInput', [
'$helper', '$timeout',
function ($helper, $timeout) {
return {
restrict : 'A',
require : '?ngModel',
replace : false,
transclude: true,
link: function($scope, $element, $attrs, $controller){
$timeout(function(){
console.log('$timeout called');
}, 500);
}
};
}]);
Upvotes: 3