实现$ compile的Angularjs + Typescript指令 [英] Angularjs+Typescript directive implementing $compile

查看:51
本文介绍了实现$ compile的Angularjs + Typescript指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下指令中注入$ compile时遇到问题.

I am having issues injecting $compile in the following directive.

export class Element {
    public link(scope:dirScopeInterface, element:any, attrs:ng.IAttributes, formCtrl:ng.IFormController) {
        var attr = this.arrayJoiner(scope.standard, scope.attrs || {}, scope.ignore || {});
        element.html(this.compiler(attr));
        $compile(element.contents())(scope);
    }
}

目前正在抛出$ compile是未定义的错误.我尝试使用

At the moment it is throwing an $compile is undefined error. I have tried using

static $inject = ['$compile'];

但是由于某种原因,它从已翻译的脚本中消失了.

But it disappears from the transpiled script for some reason.

此处是完整的代码.

推荐答案

因此,我找到了一种使其正常工作的方法,但它并不像我希望的那样优雅.

So I found a way to get it to work but it is not as elegant as I would have liked.

angular.module('formDirectives', [], function($compileProvider){
    $compileProvider.directive('catElement', ($compile) => {
        return new Element($compile);
    });
})

这篇关于实现$ compile的Angularjs + Typescript指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆