AngularJS - 运行 ng-bind-html 之后的自定义指令

AngularJS - Run custom directive after ng-bind-html

我有一个场景,我想 运行 在 ng-bind-html 创建的 DOM 上自定义指令。

基本上我必须自定义 html 标签 <a> 的行为,因为正在加载的 html 是不安全的,当用户单击 link 我必须在任何事情发生之前执行一些功能,也就是 jqLit​​e 的 click 事件。

所以我最初的想法是创建一个指令来修改我放置指令属性的容器内任何 <a> 的行为。

这工作正常,直到我与 ng-bind-html 结合,我的指令 运行s 在 ng-bind-html 之前将字符串编译成 html 并附加到 DOM.

那么,运行 我的自定义指令在 ng-bind-html 运行 之后有什么方法吗?

DEMO

HTML:

<div ng-app="myApp" ng-controller="myCtrl">
       <div ng-bind="sometext" my-directive>before</div>
    </div>

控制器:

angular.module('myApp', []);

angular.module('myApp').controller('myCtrl', function($scope) {   
   $scope.sometext="stuff here";
});

指令:

angular.module('myApp').directive('myDirective', function() { 
        return {
            priority: 10, // adjust this value ;)
            link: function(scope,element,attrs) {
                scope.$watch(attrs.ngBind, function(newvalue) {
                  console.log("element ",element.text());
                });           
            }
        };      
    });

在 mg-bind

之后使用 priority 属性 内部指令 运行 你的代码