AngularJS - Run custom directive after ng-bind-html

前端 未结 1 2025
梦如初夏
梦如初夏 2020-12-19 19:26

I\'ve a scenario which I want to run a custom directive on the DOM that ng-bind-htmlcreate.

Basicly I\'ve to customize the behavior of the html tag

相关标签:
1条回答
  • 2020-12-19 20:26

    DEMO

    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

    0 讨论(0)
提交回复
热议问题