简体   繁体   English

具有隔离范围的Angular Typescript指令在编译时生成Typescript错误

[英]Angular Typescript Directive with Isolate Scope generates Typescript errors on compile

I'm using Typescript and Angular, trying to make the site more accessible by using (among other things) the accessible icon directive below from here . 我正在使用Typescript和Angular,尝试通过(其中包括)以下此处的accessibility icon指令使网站更易于访问。

module app.directives {
    export class AccessibleIconDirective implements ng.IDirective {
        priority = 0;
        restrict = 'E';
        scope: { name: '@', text: '@'};
        template: '<i class="fa fa-{{name}}"></i><span class="invisible">{{text}}</span>';
    }
}

The Typescript compiler doesn't like the isolate scope, and gives me the following errors. Typescript编译器不喜欢隔离范围,并给我以下错误。

accessibleIcon.ts(5,24): error TS1110: Type expected.
accessibleIcon.ts(5,27): error TS1005: ';' expected.
accessibleIcon.ts(5,35): error TS1110: Type expected.
accessibleIcon.ts(8,1): error TS1128: Declaration or statement expected.

I'm not sure how to give name: '@' and text:'@' a type in this structure, and I'm at a loss why TS wants a semicolon within the scope object, or a Declaration after the module. 我不确定如何在此结构中为类型提供name: '@'text:'@' ,而我不知为什么TS希望在范围对象中使用分号,或者在模块后使用声明。

I'm implementing the ng.IDirective interface, so I would expect it to be able to deal with an isolate scope. 我正在实现ng.IDirective接口,因此我希望它能够处理隔离范围。

Any ideas? 有任何想法吗? Thanks! 谢谢!

For reference, here's the IDirective interface in angular.d.ts: 作为参考,这是angular.d.ts中的IDirective接口:

interface IDirective {
    compile?: IDirectiveCompileFn;
    controller?: any;
    controllerAs?: string;
    bindToController?: boolean|Object;
    link?: IDirectiveLinkFn | IDirectivePrePost;
    name?: string;
    priority?: number;
    replace?: boolean;
    require?: any;
    restrict?: string;
    scope?: any;
    template?: any;
    templateNamespace?: string;
    templateUrl?: any;
    terminal?: boolean;
    transclude?: any;
}

You're using : when you should be using = . 您正在使用:应该何时使用= These are supposed to be property initializers, not type annotations. 这些应该是属性初始值设定项,而不是类型注释。

module app.directives {
    export class AccessibleIconDirective implements ng.IDirective {
        priority = 0;
        restrict = 'E';
        // fixed
        scope = { name: '@', text: '@'};
        // fixed
        template = '<i class="fa fa-{{name}}"></i><span class="invisible">{{text}}</span>';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM