3
我試圖將$ compile注入到我的指令中,所以我可以在將指令附加到div之前編譯指令。現在,$ compile是不確定的,我注入錯了嗎?我使用ES6 /角1.5.x的
import MyController from './mycontroller';
class MyDirective {
constructor($compile) {
this.restrict = 'A';
this.scope = {};
this.controller = MyController;
this.controllerAs = 'vm';
this._$compile = $compile;
}
link(scope, element) {
let div = angular.element(document.getElementById('targetDiv'));
// $compile is undefined
let compiled = this._$compile("<another-directive></another-directive>")(scope));
}
static directiveFactory($compile){
MyDirective.instance = new MyDirective($compile);
return MyDirective.instance;
}
}
MyDirective.directiveFactory.$inject = ['$compile'];
export default MyDirective.directiveFactory;
我相信你必須將它注入MyDirective而不是指向directiveFactory。 –