AngularJS向链接注入$timeout?

AngularJS向链接注入$timeout?,angularjs,angularjs-directive,karma-jasmine,karma-mocha,Angularjs,Angularjs Directive,Karma Jasmine,Karma Mocha,我的问题是: 我有一个指示: autocompleteDirective.$inject = ['$timeout']; function autocompleteDirective($timeout) { return { restrict: 'E', scope: { searchParam: '=ngModel', suggestions: '=data', onType: '=onType',

我的问题是:

我有一个指示:

  autocompleteDirective.$inject = ['$timeout'];
  function autocompleteDirective($timeout) {
    return {
      restrict: 'E',
      scope: {
        searchParam: '=ngModel',
        suggestions: '=data',
        onType: '=onType',
        onSelect: '=onSelect',
        autocompleteRequired: '='
      },
      controller: autocompleteController,
      link: autocompleteLink,
      templateUrl:'modules/components/autocomplete/templates/autocomplete.html'
    };
  }
我的链接函数如下所示:

function autocompleteLink(scope, element, attrs) {

    $timeout(function() {
      scope.initLock = false;
      scope.$apply();
    }, 250);
  .... some other code 

}
和我的控制器(不太相关):

在我的link函数中,我有一个函数(目前)使用setTimeout:

  if (attrs.clickActivation) {
      element[0].onclick = function() {
        if (!scope.searchParam) {
          setTimeout(function() {
            scope.completing = true;
            scope.$apply();
          }, 200);
        }
      };
    }
我想对这段代码进行单元测试,但我的单元测试失败:

elem.triggerHandler('click');
      expect(scope.completing).to.equal(true);
尽管如此,在coverage报告中,我可以看到当单击triggerHandler时,逻辑确实成功地执行了

我认为罪魁祸首是超时

翻阅SO和其他网站,我发现使用$timeout效果最好,因为它使用了“flush()”方法

我的问题是,如何向link函数“注入”$timeout

我前面看到的示例类似于直接向指令注入$timeout,然后将link函数()嵌套在指令声明中:

function directive($timeout){
return {
  link: function(scope, attrs) {
    $timeout(blah!) //timeout is useable here...
}
}
由于我没有在指令函数中创建函数,因此上述方法对我不起作用……因此,根据我使用的模型,如何在链接中使用$timeout

function directive($timeout){
return {
  link: function(scope, attrs) {
    $timeout(blah!) //timeout is useable here...
}
}