Angularjs(Typescript)指令链接函数未触发

时间:2015-12-16 20:20:39

标签: angularjs typescript directive

我最近开始学习Typescript,我在创建指令时遇到了问题。执行工厂方法,但链接功能不执行。我不知道这是结构问题还是其他问题。

这是我的指示:

CURRENT_TIMESTAMP

提前致谢!

1 个答案:

答案 0 :(得分:0)

当我将指令添加到模块时,我忘记了()。

工作解决方案:

namespace app {
'use strict';

class MyDirective implements ng.IDirective{
    restrict: string = 'A';

    constructor(private $compile: ng.ICompileService, private $templateRequest: ng.ITemplateRequestService, private logger: blocks.logger.Logger) {}

    static factory(): ng.IDirectiveFactory {
        var directive = ($compile: ng.ICompileService, $templateRequest: ng.ITemplateRequestService, logger: blocks.logger.Logger) =>new MyDirective($compile, $templateRequest, logger);
        directive.$inject = ['$compile', '$templateRequest', 'logger'];
        return directive;
    }

    link = (scope: ng.IScope, element: ng.IAugmentedJQuery) => {

        scope.$on('$stateChangeStart',() => {
            //more code ...
        });

        scope.$on('$viewContentLoaded', ():void => {
            //more code...
        });
    }
}

angular
    .module('app')
    .directive('myDirective', MyDirective.factory()); //EDITED
}