突出显示.js在 AngularJS SPA 中不起作用

Highlight.js in AngularJS SPA doesn't work

本文关键字:SPA 不起作用 AngularJS 显示 js      更新时间:2023-09-26

我有一个AngularJS SPA,可以将文章加载到视图中。有些文章有代码示例,我想使用突出显示.js来突出显示它。

在下面的示例中,我模拟了一个 get 请求,因为这就是我在实际应用程序中加载动态内容的方式。$scope.test与我的实际应用程序可以返回的内容非常相似;一些常规的HTML打印出来,其中包括代码示例。

我的问题是:它似乎真的不起作用。

具体来说,不会突出显示任何内容。在我看来,我似乎缺少一个 init 或其他东西......哈尔普?

我也尝试过<div hljs/>相同的(缺乏)结果。没有控制台错误。

此答案提供了一种在模板中使用 ng-model 的解决方案。但是,我不在任何地方使用ng-model。

编辑:我对示例代码进行了一些更改,以进一步解释该问题。

这是我的应用程序(简化):

var app = angular.module('app', ['ngSanitize']);
app.controller('ctrl', ['$scope', '$http',
    function($scope, $http) {
        "use strict";
        $http.get('/echo/html').then(function successCallback(response) {
            $scope.title = 'Some Title';
            $scope.metaStuff = 'Written by Awesome MacFluffykins';
            $scope.articleBody = '<p>Here''s an example of a simple SQL SELECT:</p><pre><code class="sql" highlight>SELECT * FROM table WHERE user = ''1''</code></pre>';
        }, function errorCallback(response) {
            console.log("Error: %d %s", response.code, response.message);
        });
    }
]);

这是我的 HTML:

<div ng-app="app" ng-controller="ctrl">
    <h2>{{ title }}</h2>
    <p><small>{{ metaStuff }}</small></p>
    <div ng-bind-html="articleBody"></div>
</div>

最后是jsFiddle。

在我看来,最好使用这样的 DOM 操作指令。通过ng-model传递源代码(也可以使用其他属性)并在指令中运行 HLJS。由于使用异步方法向范围提供值,因此需要使用$watch来捕获值,然后运行 HLJS:

.HTML:

<div highlight ng-model="test"></div>

命令:

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

工作 JSFiddle: https://jsfiddle.net/1qy0j6qk/

小提琴https://jsfiddle.net/vg75ux6v/

var app = angular.module('app', ['hljs', 'ngSanitize']);
app.controller('ctrl', ['$scope', '$http',
    function($scope, $http) {
        "use strict";
        $http.get('/echo/html').then(function successCallback(response) {
            $scope.test = '<h2>Here''s some code:</h2><pre><code hljs class="sql">SELECT * FROM table WHERE user = ''1''</code></pre>';
        }, function errorCallback(response) {
            console.log("Error: %d %s", response.code, response.message);
        });
    }
]).directive('compile', ['$compile', function ($compile) {
    return function(scope, element, attrs) {
        scope.$watch(
            function(scope) {
                return scope.$eval(attrs.compile);
            },
            function(value) {
                element.html(value);
                $compile(element.contents())(scope);
            }
        );
    };
}]);