我是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。
答案 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">
<h1> Hello! </h1>
</code></pre>
答案 1 :(得分:0)
尚未对答案发表评论,但我发现此技术很有用。
如果您手动加载模板并将文本插入dom,Angular会自动将内容转换为HTML实体,这意味着您的原始模板仍然可读,但显示正确。
在我的应用程序中,我使用$ sce和$ templateRequest来获取模板,然后将角度模板变量设置为获取模板的值。
几点说明:
此方法还允许具有类似输出的多种类型的代码 - 例如,在我的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>
棱镜会突出它!