【问题标题】:Isolate scope in Directive with TypeScript and Angular 1使用 TypeScript 和 Angular 1 在指令中隔离范围
【发布时间】:2017-05-12 15:44:36
【问题描述】:

我在尝试使用 TypeScript 创建新的角度指令时遇到问题。仅当我计划在指令中使用隔离范围时才会出现此问题。以下是我正在做的事情:

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”错误地实现接口 '指令'。属性“范围”的类型不兼容。 输入'{方向:字符串; }' 不可分配给类型 'boolean | { [boundProperty:字符串]:字符串; }'。 '{orientation: string; 类型中缺少索引签名; }'

是的,在我的打字中ng.IDirective 具有scope?: boolean | {[boundProperty: string]: string}; 类型的范围属性

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

【问题讨论】:

    标签: javascript angularjs typescript angularjs-directive definitelytyped


    【解决方案1】:

    你试过了吗

    scope = {
                align: "="
            }
    

    【讨论】:

    • 你能发布一下你是如何使用它的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多