AngularJS API之toJson 对象转为JSON

toJson()能把对象体系化为json

AngularJS,措施讲解

本条格局最多扶助2个参数:

angular.toJson(obj, pretty);
  • obj 是想要转换的目的,
  • pretty
    可以调节格式化的样式,依照API的介绍这一个是空格的个数。(不过在控制台,空格的个数是同等的,差距还在商讨)

样例参考

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
</head>
<body ng-app="myApp">
    <div ng-controller="myCtrl">
        {{ user }}
    </div>
    <script type="text/javascript">
    angular.module("myApp",[])
    .controller("myCtrl",function($scope){
        var user = new Object();
        user.name = "xingoo";
        user.age = 36;
        user.school = "jialidun";

        console.log(user);
        console.log(angular.toJson(user));
        console.log(angular.toJson(user,2));
        console.log(angular.toJson(user,10));

        $scope.user = user;
    });
    </script>
</body>
</html>

AngularJS 1

相关文章