如何使用 TypeScript 1.6.2 在 AngularJS 1.5.4 中实现这一点?
这是官方文档中的原生 JavaScript 版本:$compile#example
我找到了这个,但它不起作用:Angularjs+Typescript 指令实现 $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);
}
}