javascript - 我如何从 AngularJS 中的编译中获得 promise ?

标签 javascript angularjs

有一个事件需要在编译完成后触发。

这是我的父 Controller 的样子:

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);
            }
        }
    }
]);

在 childCtrl 中我有一个监听器:

$rootScope.$on('event', doSomething);

问题是指令处理发生在我的 Controller 触发事件​​之后。有没有办法从编译器那里获得 promise ,或者有什么方法可以在编译发生后触发事件?

最佳答案

您可以在 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'); 
    }
  }

关于javascript - 我如何从 AngularJS 中的编译中获得 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639702/

相关文章:

javascript - 等待 canvas.toBlob() 后再继续

javascript - Telegram bot api - answerInlineQuery 中的 QUERY_ID_INVALID - Javascript

javascript - 是否有一个好的服务器端基于 javascript 的 rest API 测试框架?

javascript - Angular 返回它自己的标签而不是在 ejs 中输出

javascript - 重新加载 AngularJS Controller

javascript - Angular 嵌套 ng-repeat 在第三层不起作用

javascript - AngularJS : Reuse custom directive programmatically

javascript - 匹配小时-分钟-秒 (hms) 持续时间字符串

javascript - AngularJS:我可以使用过滤器在 ng-repeat 中分块数组吗?

javascript - AngularJS + IndexedDB,一次打开数据库供所有 Controller 使用,还是为每个请求单独打开?