如何让PrismJS在角度指令中突出显示代码块而不必实现1秒的超时延迟?

时间:2017-12-20 14:27:46

标签: angular prismjs

我创建了一个简单的应用程序(角度1.6),允许用户保存代码并使用prismjs显示代码突出显示。唯一的问题是,当我从指令中调用Prism.highlightAll时,我必须使用angular的$ timeout方法,延迟时间为1秒。

app.directive("markupPanels", function($timeout) {
return {
    restrict: "E",
    replace: true,
    templateUrl: "directives/markupPanel.html",
    link: function() {
        $timeout(function() {
            Prism.highlightAll();
        }, 1000)
    }
}

Code highlighted with 1 second delay

然而,浏览器闪烁,我认为必须有更好的方法来实现这一点。

1 个答案:

答案 0 :(得分:0)

使用element.ready()

link: function($scope, element) {
    element.ready(function() {
        Prism.highlightElement(element[0]);
    });
}

来源:angular-prism