Use json pretty print in angularjs

后端 未结 4 1474
梦如初夏
梦如初夏 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 10:45

    Angular already has the json filter built-in:

      {{data | json}}
    

    The json after the pipe | is an Angular Filter. You can make your own custom filter if you like:

    app.filter('prettyJSON', function () {
        function prettyPrintJson(json) {
          return JSON ? JSON.stringify(json, null, '  ') : 'your browser doesnt support JSON so cant pretty print';
        }
        return prettyPrintJson;
    });
    

    To use your custom prettyJSON filter:

      
        {{data | prettyJSON}}
      

    ES6 version from @TeChn4K:

    app.filter("prettyJSON", () => json => JSON.stringify(json, null, " "))
    

提交回复
热议问题