例
显示一个JavaScript对象作为JSON字符串:
<div ng-app="myApp" ng-controller="jsCtrl">
<h1>Customer:</h1>
<pre>{{customer | json}}</pre>
</div>
<script>
var app
= angular.module('myApp', []);
app.controller('jsCtrl', function($scope)
{
$scope.customer = {
"name" : "Alfreds Futterkiste",
"city" : "Berlin",
"country" :
"Germany"
};
});
</script>
试一试» 定义和用法
该json
过滤器的JavaScript对象转换成JSON字符串。
调试应用程序时,该过滤器会很有用。
JavaScript对象可以是任何类型的JavaScript对象。
句法
{{ object | json :
spacing }}
参数值
Value | Description |
---|---|
spacing | Optional. A number specifying how many spaces to user per indentation. The default value is 2 |
更多示例
例
确保JSON字符串,每个缩进12位写:
<div ng-app="myApp" ng-controller="jsCtrl">
<h1>Customer:</h1>
<pre>{{customer | json : 12}}</pre>
</div>
<script>
var app
= angular.module('myApp', []);
app.controller('jsCtrl', function($scope)
{
$scope.customer = {
"name" : "Alfreds Futterkiste",
"city" : "Berlin",
"country" :
"Germany"
};
});
</script>
试一试» 例
JavaScript对象作为一个数组:
<div ng-app="myApp" ng-controller="jsCtrl">
<h1>Carnames:</h1>
<pre>{{cars | json}}</pre>
</div>
<script>
var app
= angular.module('myApp', []);
app.controller('jsCtrl', function($scope)
{
$scope.cars = ["Audi", "BMW", "Ford"];
});
</script>
试一试» 相关页面
AngularJS教程: 角过滤器