网站搜索

javascript - 使用 TypeScript 和 Angular 1 在指令中隔离作用域


当我尝试使用 TypeScript 创建一个新的 Angular 指令时,我遇到了一个问题。这个问题仅在我打算在指令中使用隔离作用域时发生。以下是我的操作步骤:

module Foo.Layout {
    "use strict";
    class SidebarDirective implements ng.IDirective {

        templateUrl = "/layout/sidebar/sidebar.html";
        restrict = "E";
        controller = SidebarController;
        scope = {
            align: "@"
        }

        static factory() {
            var instance = () => {
                return new SidebarDirective();
            }
            return instance;
        }       
    }
    angular.module("app.layout").directive("fooSidebar", SidebarDirective.factory());
}

这个问题只在添加 scope={...} 语句时发生,否则编译正常。以下是错误信息。

错误 TS2420 类 'SidebarDirective' 实现接口 'IDirective' 时出错。属性 'scope' 的类型不兼容。类型 '{ orientation: string; }' 不能分配给类型 'boolean | { [boundProperty: string]: string; }'。类型 '{ orientation: string; }' 中缺少索引签名

是的,在我的类型定义中,ng.IDirective 有一个类型为 scope?: boolean | {[boundProperty: string]: string}; 的 scope 属性

在我的指令中创建隔离作用域的理想方法是什么?(我使用的是 angularjs.TypeScript.DefinitelyTyped 版本 6.5.5)

答案#1

你尝试过……吗

scope = {
            align: "="
        }