问题描述
我试图用prettyprint插件我angularjs应用程序。
但不能使它工作。我创建了一个简单的指令和呼叫方法prettyPrint(),但code未格式化。
FIDDLE:
App.directive(测试,函数(){
返回{
限制:'A',
链接:功能(范围,元素,ATTRS){
$(元素)。prettyPrint()
}
};
});
我修改code和我会更新在这里:
HTML
< DIV NG-应用=旋钮NG控制器=myCtrl>
< pre类=prettyprint linemus>< / pre>
< pre类=prettyprint linemus>&放大器; LT;!DOCTYPE HTML和放大器; GT;&放大器; LT; HTML LANG =EN&放大器; GT;&放大器; LT; / HTML&放大器; GT; < / pre>
< / DIV>
JavaScript的:
VAR应用= angular.module('把手',[]);
App.controller('myCtrl',函数($范围){
$ scope.dom ='和; LT;!DOCTYPE HTML和放大器; GT;&放大器; LT; HTML LANG =EN&放大器; GT;&放大器; LT; / HTML&放大器; GT;'
})App.directive('prettyprint',函数(){
返回{
限制:'C',
链接:功能postLink(范围,元素,ATTRS){
element.html(prettyPrintOne(scope.dom));
}
};
});
基本上,你需要使用文件的来控制)prettify的执行(功能,具有prettyPrintOne(),您可以在一个特定的HTML文本执行它。
和简化使用的指令,像prettify stlyle,我会建议restric到'C'类,改变指令的名称为prettyprint
I'm trying to use prettyprint plugin for my angularjs app.
But cannot make it works. I create a simple directive and call method prettyPrint(), but the code is not formatted.
FIDDLE: http://jsfiddle.net/Tropicalista/yAv4f/2/
App.directive('test', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
$(element).prettyPrint()
}
};
});
I modified your code and i'll update here:http://jsfiddle.net/yAv4f/6/
html:
<div ng-app="Knob" ng-controller="myCtrl">
<pre class="prettyprint linemus"></pre>
<pre class="prettyprint linemus"><!DOCTYPE html><html lang="en"></html></pre>
</div>
javascript:
var App = angular.module('Knob', []);
App.controller('myCtrl', function($scope) {
$scope.dom = '<!DOCTYPE html><html lang="en"></html>'
})
App.directive('prettyprint', function() {
return {
restrict: 'C',
link: function postLink(scope, element, attrs) {
element.html(prettyPrintOne(scope.dom));
}
};
});
Basically, you need to use the file prettify.js to control the execution of the prettify() function, with prettyPrintOne() you can execute it in a specific html text.
And to simplify the use of the directive, like prettify stlyle, i'll suggest restric to 'C' a class and change the the directive name to 'prettyprint'
这篇关于AngularJs如何调用prettyprint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!