首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angularjs+Typescript指令实现$compile

Angularjs+Typescript指令实现$compile
EN

Stack Overflow用户
提问于 2015-06-09 09:41:34
回答 3查看 6.2K关注 0票数 5

在下面的指令中,我遇到了注入$compile的问题。

代码语言:javascript
复制
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是未定义的错误。我试过用

代码语言:javascript
复制
static $inject = ['$compile'];

但由于某种原因,它从转写的剧本中消失了。

这里是使用的完整代码。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-06-11 12:29:01

所以我找到了一个办法让它开始工作,但它并不像我所希望的那样优雅。

代码语言:javascript
复制
angular.module('formDirectives', [], function($compileProvider){
    $compileProvider.directive('catElement', ($compile) => {
        return new Element($compile);
    });
})
票数 0
EN

Stack Overflow用户

发布于 2015-06-09 10:36:18

包括static $injectconstructor

代码语言:javascript
复制
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);
    }
}

编辑

要将这个指令注册为角,我总是这样做(有多个解决方案):

代码语言:javascript
复制
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;
    }
}

并登记:

代码语言:javascript
复制
angular.module("myModule" [])
    .directive(Element.ID, Element.factory());
票数 9
EN

Stack Overflow用户

发布于 2018-07-11 06:37:21

使用jQuery +类型脚本实现AngularJS向导步骤

对于其他$compile函数,它也应该很好地工作。

AppDirective.ts

代码语言:javascript
复制
 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

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

HTML

代码语言:javascript
复制
// 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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30728464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档