AngularJS TypeScript 指令链接函数

2024-01-27

我正在尝试使用 TypeScript 创建 AngularJS 指令。我的指令需要“ngModel”,并且我还使用在指令中注入的自定义服务。 我的主要问题是我的服务无法在链接功能中使用。

这是我想要实现的目标的示例:

module app.directives {

    export var directiveName: string = "theDirective";

    angular.module("myApp").directive(directiveName, 
           (myFactory: app.services.MyFactory) =>
           {
                return new MyDirective(myFactory);
           });

    export interface IMyDirectiveScope extends ng.IScope {
        ngModel: ng.INgModelController;
    }

    export class MyDirective implements ng.IDirective {

        restrict = "A";
        require = "ngModel";
        scope = {
            ngModel:'='
        }

        constructor(private myFactory: app.services.MyFactory) {

        }


        link(scope: IMyDirectiveScope , elem: JQuery, attributes: ng.IAttributes, ngModel: ng.INgModelController) {
            //this is window here

            elem.bind('blur', (evt: JQueryEventObject) => {  
                 //keyword this is also window here, so yeah bummer indeed
                 validate(); 
            });

            function validate() {
                 //I need to use my factory here, but I can seem to get it.
                 //this is always window and I'm kinda stuck here
            }
        }
    }
}

我似乎找不到关于这个主题的一些更高级的东西。我发现所有示例似乎都没有使用服务或复杂的链接功能。 请用某种例子来回答这个问题。你想的都是骗人的。

Update:我的链接函数中的“this”是窗口而不是“MyDirective”这一事实对我来说没有多大意义。有什么想法吗?


使用类并继承 ng.IDirective 是使用 TypeScript 的方法。

TypeScript 包含对粗箭头函数的支持() =>来自 EcmaScript 6。 这是一种速记语法,也改变了this关键词作品:

class MyDirective implements ng.IDirective {
    restrict = 'A';
    require = 'ngModel';
    scope = {
        ngModel: '='
    }

    constructor(private myFactory: app.services.MyFactory) {
    }

    link = (scope: IMyDirectiveScope, elem: JQuery, attributes: ng.IAttributes, ngModel: ng.INgModelController) => {
        console.log(this); // this points to MyDirective instance instead of Window

        elem.bind('blur', (evt: JQueryEventObject) => {
            console.log(this); // this points to MyDirective instance instead of Window
            this.validate(); 
        });
    }

    validate() {
        console.log(this); // this points to MyDirective instance instead of Window
    }


    static factory(): ng.IDirectiveFactory {
        var directive = (myFactory: app.services.MyFactory) => new MyDirective(myFactory);
        directive.$inject = ['myFactory'];
        return directive;
    }
}

app.directive('mydirective', MyDirective.factory());

你也可以依靠旧时尚var self = this;图案:

class MyDirective implements ng.IDirective {
    restrict = 'A';
    require = 'ngModel';
    scope = {
        ngModel: '='
    }

    constructor(private myFactory: app.services.MyFactory) {
    }

    link = (scope: IMyDirectiveScope, elem: JQuery, attributes: ng.IAttributes, ngModel: ng.INgModelController) => {
        console.log(this); // this points to MyDirective instance instead of Window

        var self = this;

        function validate() {
            console.log(self); // self points to MyDirective instance
        }

        elem.bind('blur', function(evt: JQueryEventObject) {
            console.log(self); // self points to MyDirective instance
            validate(); 
        });
    }
}

相关回答:https://stackoverflow.com/a/29223535/990356 https://stackoverflow.com/a/29223535/990356

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularJS TypeScript 指令链接函数 的相关文章

随机推荐

  • Jersey - 为资源方法注册 ExceptionMapper

    问题特定类的异常处理 映射 https stackoverflow com questions 36370991让我想到了如何注册的问题ExceptionMapper到特定资源Method 我尝试过使用DynamicFeature像这样 动
  • 对 node.js、connect-mongo 的并行请求、会话被覆盖

    在当前的项目 一种商店系统 中 我使用node js快递JS and 连接 mongo作为会话存储 在客户端 我在启动时使用单个请求来创建一个新会话 然后向 Node js 服务器发送多个并行请求 当然 因为这些并行请求会更改会话 所以这些
  • Golang:JSON:如何将字符串数组解组为 []int64

    Golang encoding json包让你使用 string结构标记以便编组 解组字符串值 例如 309230 into int64场地 例子 Int64String int64 json string 但是 这不适用于切片 即 int
  • 与 join 一起计数

    我想找出哪个播放列表包含超过 2 首歌曲 该语句有效 但我想要播放列表的名称和显示的歌曲的 count 我想我必须使用连接 但我不明白它应该如何工作 有人可以帮忙吗 playlist table id name playlist songs
  • 从具有 NaN 的多维数组中找出最小值

    我有一个二维数组 double 我想知道最小值是多少 我尝试了 Linq Select Min 但由于我的数组通常包含NaN值 那么minvalue总是NaN 因此 我需要某种方法来找到 跳过 NaN 的最小值 任何帮助深表感谢 今天是扩展
  • 反向应用提交到工作副本

    为了研究先前提交引入的效果 我想将其反向应用到我的工作副本并修改代码 我管理了围绕创建和应用补丁的工作流程 但想知道这是否可以更容易地完成 git checkout b tmp fiddle git diff R p d9fd2bb d9f
  • JSF Converter 错误消息中的自定义变量

    我有一个表单页面 其中有一个接受日期的 inputText 字段 我们有一个转换器 可以将文本框中的字符串转换为 Date 对象 即 2011 03 01 到 java util Date 2011 03 01 如果字符串不是日期 例如 1
  • 带 URL 操作的 JSF 表单?

    有没有什么方法可以调用 URL 操作
  • 使用 java UrlConnection 通过 ntlm(或 kerberos)进行身份验证

    我需要使用 java 使用 REST Web 服务 传递域用户帐户的凭据 现在我正在用经典的asp来做 set xmlHttp server createObject msxml2 serverxmlhttp xmlHttp open me
  • 如何在 django 中禁用南调试日志记录?

    当我在 Django 中运行测试时 失败后我从 South 获得了几页调试输出 如下所示 south DEBUG south execute CREATE INDEX sometable 4d5bad5 ON video playable
  • Rails 删除链接 JavaScript ajax 调用

    我想创建一个ajax删除调用 单击链接时 应出现确认框 然后 p 标签淡出 注释 问题在于ajax调用应该如何以及如何显示确认框 HTML 视图 a class softdelete href blogs 5 comments 18 sle
  • 如何在 Core 2.0 的 ConfigurationBuilder 中设置BasePath

    如何在 Core 2 0 的 ConfigurationBuilder 中设置基本路径 我用谷歌搜索并发现this https stackoverflow com questions 33169589 specify the applica
  • 使用密钥保管库的更安全方式

    通常 当您使用密钥保管库加密和解密数据时 您必须以纯文本形式将 AD 注册应用程序 有权访问密钥保管库 的 ClientID 和 ClientSecret 保存在某处 如果有人窃取了 ClientID 和 Secret 那么任何人都可以声称
  • 如何设置机器人的状态

    所以我试图让我的机器人流媒体与抑郁症 但我已经尝试了多种方法 但它们不起作用 我尝试过这些方法 client user setPresence game name with depression status online bot user
  • 我可以在 BigQuery 中检索外部表数据的文件名吗?

    希望为部门团队实现一个简单的数据存储 他们目前在其中管理大量 excel csv 文件 我们将让他们准备文件并将它们以 CSV 格式放入 GCS 存储桶中 然后将外部 BQ 表指向此 一切都很好 但是 如果他们运行查询并看到一些数据 然后想
  • 在 Objective C、.net 和 Android 中生成相同的加密字符串 AES/CBC/PKCS7Padding

    我想在iOS中生成相同的加密字符串 在 net中生成android 我可以在 android 和 net 中生成相同的字符串 但对于 Objective C 则不同 安卓代码 public static String encrypt Str
  • VS 安装项目:安装时卸载其他组件

    我正在创建一个 Visual Studio 安装项目 我想从安装我的组件时从系统中卸载另一个组件 另一个组件是从我自己使用 Visual Studio 创建的设置安装的 目前 当我从组件的安装操作中调用另一个组件的卸载时 我收到错误代码 1
  • 为什么带有 pop 方法(或 del 语句)的 for 循环不迭代所有列表元素[重复]

    这个问题在这里已经有答案了 我是 Python 新手 正在尝试使用列表 我在 linux2 上使用 Python 3 2 3 默认 2012 年 10 月 19 日 20 13 42 GCC 4 6 3 这是我的示例代码 gt gt gt
  • 确保 C# 中静态方法的线程安全

    我目前在静态类 方法中有一些代码 但我想检查它是否是线程安全的 从我读到的内容来看 我认为这应该没问题 但我内心深处的某些想法告诉我 这可能不是 我的网页的数据处理阶段使用外部 Web 服务来创建订单记录 这可能会非常慢 可能需要 30 4
  • AngularJS TypeScript 指令链接函数

    我正在尝试使用 TypeScript 创建 AngularJS 指令 我的指令需要 ngModel 并且我还使用在指令中注入的自定义服务 我的主要问题是我的服务无法在链接功能中使用 这是我想要实现的目标的示例 module app dire