突出显示在外部模板中不起作用

时间:2015-02-02 17:21:44

标签: angularjs prism.js

我是angularjs的菜鸟,我有一个问题。 我在我的网站上使用prism.js或highlight.js(相同的结果)。它在index.html中正常工作,但它在我使用ngRoute加载的其他模板中不起作用。 我相信问题是angularjs只是它再次渲染html并且当我加载我的content-principal.html时它不起作用。

INDEX.HTML

//<pre><code class="language-javascript">
    colour syntax is ok
//</code></pre>

APP.JS

ionicEsApp.config(function($routeProvider) {
    $routeProvider.
    when('/', {
        templateUrl: 'templates/content-principal.html',
        //controller: 'IonicEsController'
 }).

内容principal.html

//<pre><code class="language-javascript">
    colour syntax is NO work
//</code></pre>

¿任何解决方案?谢谢,抱歉我的英文:P。

3 个答案:

答案 0 :(得分:2)

解决。

我们需要:

<强>的index.html

来自http://prismjs.com/#basic-usage

prims.js和prism.css

<强> app.js

创建新指令(在.conf之前非常重要)

var ionicEsApp = angular.module('ionicEsApp', [
    'ngRoute',
    'ngResource',
    'ionicEsController'
]);

ionicEsApp.directive('ngPrism', [function() {
    return {
        restrict: 'A',
        link: function($scope, element, attrs) {
            element.ready(function() {
                Prism.highlightElement(element[0]);
            });
        }
    }
}]);

ionicEsApp.config(function($routeProvider) {
    $routeProvider.
    when('/', {
        templateUrl: 'templates/content-principal.html',
        //controller: 'IonicEsController'
    }).
    otherwise({
        redirectTo: '/'
    });

});

<强> 内容principal.html

我们必须将新指令用于代码标记。

<pre><code ng-prism class="language-javascript">
    alert("Prims is ok");
</code></pre>

注意:html存在问题,我们需要更换&lt;符号由&amp; lt。例如:

<pre><code class="language-markup">
&lth1> Hello! &lt/h1>
</code></pre>

答案 1 :(得分:0)

尚未对答案发表评论,但我发现此技术很有用。

如果您手动加载模板并将文本插入dom,Angular会自动将内容转换为HTML实体,这意味着您的原始模板仍然可读,但显示正确。

在我的应用程序中,我使用$ sce和$ templateRequest来获取模板,然后将角度模板变量设置为获取模板的值。

几点说明:

  • 每个指令实例都有多个代码示例,由codeType变量
  • 标识
  • 我的模板文件名采用_ {codeType} .sample的形式,例如_css.sample
  • 模板位置作为dom
  • 中指令的属性传入
  • dom元素容器由类.sample- {codeType}标识,例如.sample-css
  • 角度占位符由{{sample {codeType}}标识,例如{{samplecss}}
  • 为了防止竞争条件,我使用$ timeout来等待节拍并允许当前的$ apply()完成,然后在代码上调用Prism。

此方法还允许具有类似输出的多种类型的代码 - 例如,在我的styleguide中,我显示输出HTML(codeType ='html')和未呈现的Angular模板(codeType ='ng') - 两者都需要Prism .language-markup类。

如果每个指令只有一个代码示例,这可以简化很多。

function StyleGuideComponentController($scope, $element, $templateRequest, $sce, $timeout)
{
    var codeSampleTypes =
    [
        'html',
        'ng',
        'ngjs',
        'css',
        'less'
    ];

    insertAllCodeSamples();

    function insertAllCodeSamples()
    {
        var key;

        for (key in codeSampleTypes)
        {
            insertCodeSample(codeSampleTypes[key]);
        }
    }

    function insertCodeSample(codeType)
    {
        var sampleUrl = $scope.templateLocation + '/_' + codeType + '.sample',
            sampleCode = $sce.getTrustedResourceUrl(sampleUrl);

        $templateRequest(sampleCode).then(function(template)
        {
            var codeElement    = $element.find('.sample-' + codeType)[0],
                prismLanguage      = codeType,
                prismLanguageTypes =
                {
                    'html' : 'markup',
                    'ng'   : 'markup',
                    'js'   : 'javascript',
                    'ngjs' : 'javascript'
                },
                key;

            for (key in prismLanguageTypes)
            {
                if (prismLanguage === key)
                {
                    prismLanguage = prismLanguageTypes[key];
                }
            }

            codeElement.className += ' language-' + prismLanguage;

            $scope['sample' + codeType] = template;

            $timeout(function()
            {
                Prism.highlightElement(codeElement);
            });
        }, function()
        {
            $scope['sample' + codeType] = 'An error occurred' +
            ' while fetching the code sample';
        });
    }

    return {
        restrict   : 'E',
        scope      :
        {
            templateLocation: '='
        },
        controller : StyleGuideComponentController
    };
}

答案 2 :(得分:0)

如果您使用ng-view加载模板,有一种非常简单的方法:

如果您有这样的事情:

<div ng-controller="MainCtrl">
     <div id="content" ng-view>
     </div>
</div>

您可以将它添加到您的MainCtrl控制器:

$scope.$on('$viewContentLoaded', function(){
    Prism.highlightAll();
});

现在,如果您使用默认方式突出显示代码:

<pre><code class="language-javascript">
     Prism will highlight it
</code></pre>

棱镜会突出它!