【问题标题】:Angular Typescript Directive with Isolate Scope generates Typescript errors on compile带有 Isolate Scope 的 Angular Typescript 指令在编译时生成 Typescript 错误
【发布时间】:2016-02-16 23:12:31
【问题描述】:

我正在使用 Typescript 和 Angular,试图通过使用下面来自here 的可访问图标指令(除其他外)使网站更易于访问。

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>';
    }
}

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.

我不知道如何在这个结构中给 name: '@'text:'@' 一个类型,我不知道为什么 TS 想要在作用域对象中使用分号,或者在模块之后使用声明。

我正在实现 ng.IDirective 接口,所以我希望它能够处理隔离范围。

有什么想法吗?谢谢!

作为参考,这里是 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;
}

【问题讨论】:

    标签: angularjs typescript isolate-scope


    【解决方案1】:

    当你应该使用 = 时,你正在使用 :。这些应该是属性初始化器,而不是类型注释。

    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>';
        }
    }
    

    【讨论】:

    • 我要说的是“天哪!”还有“谢谢”。无法看到使用 3 框架树的森林
    猜你喜欢
    • 2017-08-06
    • 2015-10-25
    • 2016-04-02
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多