[英]AngularJS - Run custom directive after ng-bind-html
我有一個場景,我想在ng-bind-html
創建的DOM上運行自定義指令。
基本上我要自定義html標簽<a>
的行為,因為加載的html是不安全的,當用戶點擊鏈接時我會在發生任何事情之前執行某些功能,即jqLite的click
事件。
所以我的原始ideia是創建一個指令,修改我放置指令屬性的容器內的任何<a>
的行為。
這很好用,直到我與ng-bind-html
結合使用,我的指令在ng-bind-html
編譯之前運行,然后將字符串編譯成html並附加到DOM。
那么,在ng-bind-html
運行之后運行我的自定義指令有什么方法嗎?
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());
});
}
};
});
使用priority
屬性inside指令在mg-bind之后運行代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.