sta*_*lar 5 javascript angularjs
编译完成后需要触发一个事件。
这是我的父控制器的样子:
myApp.directive("ParentDirective", ["$rootScope", "$compile",
function($rootScope, $compile) {
return {
link: function (scope, element) {
var onClick = function () {
if (!scope.childDir) {
scope.childDir = $compile('<div ng-controller="childCtrl"/>')(scope);
$(scope.element).append(scope.childDir);
}
$rootScope.$broadcast('event');
}
$(element).click(onClick);
}
}
}
]);
Run Code Online (Sandbox Code Playgroud)
在 childCtrl 我有一个听众:
$rootScope.$on('event', doSomething);
Run Code Online (Sandbox Code Playgroud)
问题是指令处理发生在我的控制器触发事件之后。有没有办法从编译器获得承诺或在编译发生后以任何方式触发事件?
您可以在postLink函数中触发该事件。尝试一下。
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) {
$rootScope.$broadcast('event');
}
}
Run Code Online (Sandbox Code Playgroud)