Javascript Angular TypeScript指令';s链接方法不使用';我没接到电话

Javascript Angular TypeScript指令';s链接方法不使用';我没接到电话,javascript,angularjs,typescript,angularjs-directive,Javascript,Angularjs,Typescript,Angularjs Directive,我正在尝试实现一个基于角色的访问控制系统,允许的资源将在登录后从服务器加载。我可以使用原始JavaScript代码检查它 angular.module('app').directive('accessControl', [ 'AuthService', function (authService) { return { restrict: 'A', scope: "=", link: function

我正在尝试实现一个基于角色的访问控制系统,允许的资源将在登录后从服务器加载。我可以使用原始JavaScript代码检查它

angular.module('app').directive('accessControl',
[
    'AuthService', function (authService) {
        return {
            restrict: 'A',
            scope: "=",
            link: function (scope, element, attrs) {
                scope.canShow = function(resource) {
                    var allowedResources = authService.accountInfo.resources;
                    return allowedResources.indexOf(resource) !== -1;
                }

            }
        }
    }
]); 
但由于我的整个应用程序都是用TypeScript编写的,所以我一直试图用纯TypeScript编写指令,但不幸的是,我无法这样做。这是我的TS代码

export class AccessControl implements ng.IDirective {

    public authService: App.AuthService;
    public link: (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => void;

    constructor(authService: App.AuthService) {
        this.authService = authService;
        console.log('authservice: ', authService);
        AccessControl.prototype.link = (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => {
            scope["canShow"] = function (resource: string) {
                // some logic
                console.log('can show' + resource);
                return true;
            };
        };
    }

    public static factory(): ng.IDirectiveFactory  {
        var directive = (authService: App.AuthService) => {
            return new AccessControl(authService);
        };

        directive['$inject'] = ['AuthService'];
        return directive;
    }

    restrict = "A";
    scope: "=";
}

angular.module('app').directive('accessControl', AccessControl.factory());
链接函数永远不会被调用。
任何帮助或指点都将不胜感激

我们一直将
链接声明为类上的一个公共函数。在指令类上不需要公共
scope
变量,除非您使用的是隔离作用域(在这种情况下,它将是一个带有每个特定传递的作用域变量或方法的对象)。此外,您可以直接在
指令
上设置
$inject
,而无需使用括号属性符号。以下是我们如何使用TypeScript创建指令:

namespace app.directives {
    export class AccessControl implements ng.IDirective {

        public restrict = "A";

        constructor(private authService: App.AuthService) {
            console.log("authservice: ", this.authService);
        }

        public link(scope: ng.IScope, 
                    element: ng.IAugmentedJQuery, 
                    attrs: ng.IAttributes) {
            console.log("in link function of directive", scope);
        }

        public static factory(): ng.IDirectiveFactory  {
            var directive = (authService: App.AuthService) => {
                return new AccessControl(authService);
            };

            directive.$inject = ["AuthService"];
            return directive;
        }
    }

    angular.module("app")
        .directive("accessControl", AccessControl.factory());
}