Use json pretty print in angularjs

后端 未结 4 1510
梦如初夏
梦如初夏 2021-01-30 10:32

How can I use this json pretty print [ http://jsfiddle.net/KJQ9K/ ] with angularJS?

Lets assume myJsonValue is

{a:1, \'b\':\'foo\', c:[false,\'false\',n         


        
4条回答
  •  我在风中等你
    2021-01-30 10:44

    You have a few options. What I consider the most "AngularJS" way is to wrap your custom object into an Angular service:

    myAngularModule.service('myPrettyPrintService', ,myObject );
    

    The inject that into a controller:

    myAngularModule.controller('myTestController', function(myPrettyPrintService){}
    

    Then inside the controller, reference the functions and sort:

    myPrettyPrintService.syntaxHighlight();
    

    Since anything defined in JavaScript, is global anyway you could technically just access it inside a controller:

    syntaxHighlight();
    

    That may screw up with unit testingt because it adds a external, undefined, dependency to your controller.

提交回复
热议问题