What is AngularJS json filter?
AngularJS Json
filter allows you to convert a JavaScript object into JSON string.
According to
angularJS.org developer guide document json filter is mostly useful for
debugging. When using the double curly notation the binding is automatically
converted to JSON.
Syntax
In HTML template
{{json_expression | json : spacing}}
In JavaScript
$filter('json')(object,spacing)
Parameter Value
|
Description
|
object
|
Any JavaScript object (including
arrays and primitive types) to filter.
|
Spacing(Optional)
|
Optional parameter, the number of
spaces to use per indentation, defaults to 2.
|
Example
<body>
<div ng-app="myApp" ng-controller="EmployeeController">
<!--json object -->
<pre>{{Employee
|
json}}</pre>
<!--json filter with spacing -->
<p>{{Employee
|
json
:
12}}</p>
</div>
<script>
var app =
angular.module('myApp', []);
var dateOut = new Date('1985-04-05 12:08:15.773');
app.controller('EmployeeController', ['$scope', function ($scope) {
$scope.Employee = {
id: 1,
userName: 'sKendre;',
name: 'Shourya Kendre',
BirthDate:
dateOut,
Package: 52503.30,
Designation: 'Team Leader',
Payment: 700,
Projects: [
{ id: 1, name:
'E shop', TeamSize: 5, isActive: true },
{ id: 2, name:
'Employee Notifier', TeamSize: 3, isActive: true },
{ id: 3, name:
'CRM',
TeamSize: 10, isActive: false }
]
};
}]);
</script>
</body>