用于编译的TypeScript AngularJS指令?

时间:2016-04-21 07:07:02

标签: angularjs angularjs-directive typescript angularjs-compile angularjs-config

如何使用TypeScript 1.6.2在AngularJS 1.5.4中实现此功能?

以下是官方文档中的vanilla JavaScript版本:$compile#example

我发现了这个,但它不起作用:Angularjs+Typescript directive implementing $compile

这是我目前的尝试:

export class Element implements angular.IDirective {
  public static ID = 'element';

  static $inject = ['$compile'];

  static factory(): angular.IDirectiveFactory {
    const directive = ($compile: ng.ICompileService) => new Element($compile);
    directive.$inject = Element.$inject;
    return directive;
  }

  constructor(private $compile: ng.ICompileService) {
  };

  link(scope: ng.IScope, element: any, attrs: any) {
    return scope.$watch(
      (scope: ng.IScope) => scope.$eval(attrs.compile),
      (value: string) => {
        element.html(value);
        this.$compile(element.contents())(scope);
      }
    );
  }

  link_old_attempt(scope: any, element: any, attrs: ng.IAttributes,
                   formCtrl: ng.IFormController) {
    const attr = scope.standard || scope.attrs || scope.ignore;
    element.html(this.$compile(attr));
    this.$compile(element.contents())(scope);
  }
}

0 个答案:

没有答案