简体   繁体   English

在Typescript中错误地为角度指令键入了服务对象

[英]Service object wrongly typed for angular directive in Typescript

Below is typescript for an angular directive. 以下是角度指令的打字稿。 The problem is with the injected service "datacontext". 问题在于注入的服务“ datacontext”。 The debugger shows that the datacontext in the constructor is a WINDOW object, not the datacontext object that was created as a service. 调试器显示,构造函数中的datacontext是WINDOW对象,而不是作为服务创建的datacontext对象。 Therefore, in the scope.viewRecord function, the datacontext.cancelChanges() function is, obviously, undefined - - as its not part of a WINDOW object This is probably some strange scoping issue that I just don't get, but I'm at a loss as to how to debug this. 因此,在scope.viewRecord函数中,datacontext.cancelChanges()函数显然是未定义的-因为它不是WINDOW对象的一部分这可能是一些我不了解的奇怪范围问题,但是我不知如何调试它。 Any insight would be most appreciated. 任何见解将不胜感激。 Thanks. 谢谢。

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

The error seems to be at the place where you register this directive. 错误似乎在您注册此指令的位置。 Make sure it is like : 确保它像:

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

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

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