假设我有以下两个指令:
angular.module('test').directive('apiDirective', function (){
return {
restrict: 'A',
controller: function () {
this.awesomeFunction = function () {
console.log("CHECK ME OUT BEING AWESOME");
}
}
}
});
angular.module('test').directive('consumerDirective', function ($compile) {
return {
restrict: 'E',
require: '?apiDirective',
transclude: true,
link: function (scope, element, attrs, controller, transcludeFn) {
console.log('preLink controller: ', controller);
transcludeFn(scope, function (tElem, tScope) {
if (attrs.apiDirective === undefined) {
element.attr('api-directive', '');
$compile(element)(scope);
}
element.html(tElem);
$compile(tElem)(tScope);
});
scope.consumerFunction = function () {
console.log('trying to consume the awesome', controller);
controller.awesomeFunction();
}
}
}
});
以及以下html:
<consumer-directive ng-click="consumerFunction()"/>
当指令加载转换按预期发生时,将控制器设置为我的 apiDirective 控制器。但是在 consumerFunction 中,控制器始终是null
. 为什么?我怀疑它与第二次编译有关?
完全删除第二个编译会导致指令无法正常呈现。为什么?
发生了什么,如何在不手动将属性级别控制器内联到指令上的情况下修复它?