<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>angular.toJson</title> </head> <body ng-app="toJsonExample"> <h1>angular.toJson</h1> <p> <span>描述:</span><br /> <span>把对象转换为Json字符串</span> </p> <p> <span>使用方法:</span><br /> <span>angular.toJson(obj, pretty)</span> </p> <p> <span>参数:</span><br /> <span>obj:object, 要转换的对象</span><br /> <span>pretty:boolean || number, 如果设置为true,json输出将包含换行符和空格。如果设置为一个整数,json输出将包含许多空间缩进</span><br /> </p> <p> <span>返回值:</span><br /> <span>string || undefined</span> </p> <hr /> <div ng-controller="toJsoncon"> <pre id="test">{{vals}}</pre> </div> </body> <script src="../../js/angular.1.3.0.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> angular.module('toJsonExample', []).controller('toJsoncon', ['$scope', function($scope) { var obj = { name: '张三', age: '18岁' } $scope.vals = angular.toJson(obj); console.log(typeof $scope.vals) }]) </script> </html>