2014-12-01 48 views
2

在Angular指令中获得$ timeout的时间非常可怕。基本上,即使是最简单的实现继续抛出TypeError:数字不是函数错误。

我已经做了一些研究,对于我的生活无法弄清楚什么是错的。

$timeout(function(){ 
    console.log('$timeout called'); 
}, 500); 

我只剩下假设$超时期望延迟的功能,这是没有意义的,除非我正在阅读文档不正确。

https://docs.angularjs.org/api/ng/service/ $超时

在此先感谢。

从指令中除去$ timeout实现之外的所有内容。

(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); 
+5

看起来干净在顶部被注入,非常喜欢你的$帮手,由指令名,进入功能以及我。你确定'$ timeout'没有被注入的某种方式覆盖吗?也许你想把所有的代码放在这里? – deitch 2014-12-01 18:35:44

+1

更具体地说,你能说明在你的指令中如何定义'$ timeout'依赖关系吗? – raina77ow 2014-12-01 18:38:33

+1

那么,它适用于我:[小提琴](http://jsfiddle.net/efutf7f2/) – lante 2014-12-01 18:40:21

回答

3

$超时需要,因为你正在使用的阵列形式

.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); 

    } 
    }; 
}]); 
+0

谢谢,这确实是解决我的问题。干杯。 – 2014-12-01 18:48:10

+1

working plunker:http://plnkr.co/edit/VIGWnvK9OsJ265npWPwW?p=preview – SoluableNonagon 2014-12-01 18:48:10