AngularJs如何调用prettyprint? [英] AngularJs how to call prettyprint?

查看:88
本文介绍了AngularJs如何调用prettyprint?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用prettyprint插件我angularjs应用程序。

但不能使它工作。我创建了一个简单的指令和呼叫方法prettyPrint(),但code未格式化。

FIDDLE: http://jsfiddle.net/Tropicalista/yAv4f/2/

  App.directive(测试,函数(){
返回{
    限制:'A',
    链接:功能(范围,元素,ATTRS){
        $(元素)。prettyPrint()
    }
};
});


解决方案

我修改code和我会更新在这里:
http://jsfiddle.net/yAv4f/6/

HTML

 < D​​IV 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.js 来控制)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">&lt;!DOCTYPE html&gt;&lt;html lang="en"&gt;&lt;/html&gt;</pre>
</div>

javascript:

var App = angular.module('Knob', []);
App.controller('myCtrl', function($scope) {
    $scope.dom = '&lt;!DOCTYPE html&gt;&lt;html lang="en"&gt;&lt;/html&gt;'
})

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆