Angular JS: Detect if ng-bind-html finished loading then highlight code syntax

后端 未结 3 1979
陌清茗
陌清茗 2021-01-13 11:00

I am using ng-bind-html for binding data that I get from database.

app.controller(\'customersC
3条回答
  •  萌比男神i
    2021-01-13 11:50

    This is where directives come in very handy. Why not append the HTML yourself and then run the highlighter?

    Template:

    Directive:

    .directive('highlight', [
        function () {
            return {
                replace: false,
                scope: {
                    'ngModel': '='
                },
                link: function (scope, element) {
                    element.html(scope.ngModel);
                    var items = element[0].querySelectorAll('code,pre');
                    angular.forEach(items, function (item) {
                        hljs.highlightBlock(item);
                    });
    
                }
            };
        }
    ]);
    

    Example: http://plnkr.co/edit/ZbcNgfl6xL2QDDqL9cKc?p=preview

提交回复
热议问题