Search code examples
jsonangularjsstringify

JSON stringify with AngularJS on the fly transformation


Why can I not use something like this?

<input type="text" ng-model="testVal">
<pre>{{JSON.stringify(testVal,null,4)}}</pre>

If I remove the JSON.stringify() method my code works just fine.


Solution

  • You can simply use | json :

    {{ testVal | json }}

    or add JSON to $scope :

    $scope.JSON = JSON 
    

    then

    <pre>{{ JSON.stringify(testVal) }}</pre>
    

    works

    http://plnkr.co/edit/PPXK36BbTslkK5FjfP4x?p=preview