TypeScript AngularJS directive for compile?

1k Views Asked by At

How do I implement this in AngularJS 1.5.4 with TypeScript 1.6.2?

Here is the vanilla JavaScript version from the official docs: $compile#example

I've found this, but it doesn't work: Angularjs+Typescript directive implementing $compile

Here lies my current attempt:

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

There are 0 best solutions below