简体   繁体   English

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

[英]AngularJS - Run custom directive after ng-bind-html

I've a scenario which I want to run a custom directive on the DOM that ng-bind-html create. 我有一个场景,我想在ng-bind-html创建的DOM上运行自定义指令。

Basicly I've to customize the behavior of the html tag <a> because the html that is loading is unsafe and when the user click the link I've to execute some functions before anything happens, aka, the click event of jqLite. 基本上我要自定义html标签<a>的行为,因为加载的html是不安全的,当用户点击链接时我会在发生任何事情之前执行某些功能,即jqLit​​e的click事件。

So my original ideia was create a directive that modifies the behavior of any <a> inside the container that I put the attribute of my directive. 所以我的原始ideia是创建一个指令,修改我放置指令属性的容器内的任何<a>的行为。

This works fine, until I combine with ng-bind-html , my directive runs before the ng-bind-html compile the string into html and attached to the DOM. 这很好用,直到我与ng-bind-html结合使用,我的指令在ng-bind-html编译之前运行,然后将字符串编译成html并附加到DOM。

So, is any way to run my custom directive after the ng-bind-html run? 那么,在ng-bind-html运行之后运行我的自定义指令有什么方法吗?

DEMO DEMO

HTML: HTML:

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

Controller: 控制器:

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

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

Directive: 指示:

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

Use priority property inside directive to run your code after mg-bind 使用priority属性inside指令在mg-bind之后运行代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM