Typescript中为angular指令输入错误的服务对象

Service object wrongly typed for angular directive in Typescript

本文关键字:服务 对象 错误 输入 中为 angular 指令 Typescript      更新时间:2023-09-26

下面是一个angular指令的typescript:问题出在被注入的服务"datcontext"上。调试器显示构造函数中的数据上下文是一个WINDOW对象,而不是作为服务创建的数据上下文对象。因此,在范围内。viewRecord函数,datacontext.cancelChanges()函数显然是未定义的——因为它不是WINDOW对象的一部分。这可能是一些奇怪的范围问题,我只是没有得到,但我对如何调试这个感到茫然。任何见解将是最感激的。谢谢。

module app {
    export interface IEditButtonGroup {
        ...
    }
    export interface IEditButtonScope extends ng.IScope {
        ...
    }
    export class PrxEditButtonGroup implements IEditButtonGroup {
        public static $inject: Array<string> = [
            "datacontext"
        ];
        constructor(
            public datacontext: IDataContext, <---- datacontext HERE is typed as a WINDOW object
            public directive: ng.IDirective = {}) {
            directive.templateUrl = "app/directives/templates/editbuttongroup.html",
            directive.restrict = 'E';
            directive.link = (scope: IEditButtonScope, element, attr) => {
                scope.isEditing = false;
                scope.isAdding = false;
                $("form.disabled").find("input:not(:disabled), select:not(:disabled), textarea:not(:disabled)").prop("disabled", true);
                scope.editRecord = () => {
                    $("input, select, textarea").removeAttr("disabled");
                    scope.isEditing = true;
                    scope.afterEdit();
                }
                scope.viewRecord = (afterCancel: boolean) => {
                    datacontext.cancelChanges(); <--- HERE TOO!  Debugger says datacontext = WINDOW object
                    scope.isEditing = scope.isAdding = false;
                    $("form.disabled").find("input:not(:disabled), select:not(:disabled), textarea:not(:disabled)").prop("disabled", true);
                    scope.afterAdd();
                }
            }
            return <any>directive;
        }         
    }
}

错误似乎是在你注册这个指令的地方。确保它像这样:

mymodule.directive('prxEditButtonGroup',app.PrxEditButtonGroup )