AngularJS和TypeScript指令实现$compile

5
我在以下指令中注入$compile遇到了问题。
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未定义”的错误。我已经尝试使用

static $inject = ['$compile'];

但由于某些原因,它在转译后的脚本中消失了。 这里 是使用的完整代码。
3个回答

9

需要包含static $inject和一个constructor

export class Element {

    // $compile can then be used as this.$compile
    constructor(private $compile: ng.ICompileService){};

    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));
        this.$compile(element.contents())(scope);
    }
}

编辑

要在Angular中注册此指令,我通常会执行以下操作(有多种解决方案):

export class Element implements angular.IDirective {

    public static ID = "element";

    // This can then be removed:
    // static $inject = ["$compile"];

    // ..

    /**
     * The factory function that creates the directive
     */
    static factory(): angular.IDirectiveFactory {
        const directive = ($compile) => new Element($compile);
        directive.$inject = ["$compile"];
        return directive;
    }
}

并且注册:

angular.module("myModule" [])
    .directive(Element.ID, Element.factory());

我该如何在Angular中注册这个指令,因为构造函数需要一个参数?另外,我按照您指定的方式实现了它,但是出现了“$compile不是一个函数”的错误。 - Rockroxx
Angular使用依赖注入,因此构造函数将自行解析。您必须使用指向控制器的指针才能访问私有的$compile变量:this.$compile - devqon
看起来像是这样: static $inject = ['$compile']; constructor(private $compile:ng.ICompileService){} public link = (scope:dirScopeInterface, element:any, attrs:ng.IAttributes, formCtrl:ng.IFormController) => { scope.tempForm = formCtrl; var attr = this.arrayJoiner(scope.standard, scope.attrs || {}, scope.ignore || {}); element.html(this.compiler(attr)); this.$compile(element.contents())(scope); } Angular 抛出了一个 $compile 不是函数的错误。 - Rockroxx
我已经添加了我如何注册此类 TypeScript 指令的方式。 - devqon
@devqon: 我在哪里找到Directive,以便在new Directive中使用? - A T
抱歉,那是一个打错的字,应该是 new Element() - devqon

0

所以我找到了一种让它工作的方法,但并不像我想要的那样优雅。

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

0

我的实现是使用AngularJS + Typescripts编写的jQuery向导步骤

它也应该适用于其他$compile函数。

AppDirective.ts

 export class stepwizard implements ng.IDirective {
    constructor() {
        console.log("construtor step wizard directive");
    }

    link($scope: ng.IScope, $element: JQuery, attributes: ng.IAttributes, ngModel: ng.INgModelController, ctrl: any) {
        console.log("start step wizard link function");
        $element.wrapInner('<div class="steps-wrapper">');
        const steps = $element.children(".steps-wrapper").steps({
            headerTag: "h3",
            bodyTag: "section",
            transitionEffect: "slideLeft",
            autoFocus: true
        });
        const compiled = ctrl.$compile(steps);
    }

    public static Factory() {
        var directive = () => {
            return new stepwizard();
        };

        directive.$inject = ['$compile'];
        console.log("initial step wizard");

        return directive;
    }
}

AppController.ts

    export class pageController{
    // your declaraction
    static $inject: Array<string> = [
      $compile',
    ];
    constructor(
      $compile: ng.ICompileService,
    ) {
    // your constructor declaraction
    }

HTML

// sample take from official website
             <div stepwizard>
                <h3>Keyboard</h3>
                <section>
                    <p>Try the keyboard navigation by clicking arrow left or right!</p>
                </section>
                <h3>Effects</h3>
                <section>
                    <p>Wonderful transition effects.</p>
                </section>
                <h3>Pager</h3>
                <section>
                    <p>The next and previous buttons help you to navigate through your content.</p>
                </section>
            </div>

directives.stepwizard = <namespace>.directives.stepwizard.Factory();
app.directive(directives);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接