Skip to content
Related Articles

Related Articles

AngularJS | angular.toJson() Function
  • Last Updated : 12 Apr, 2019

The angular.toJson() Function in AngularJS is used to serialize the javascript object into a JSON – formatted string. It takes the javascript object and returns JSON string.

Syntax:

angular.toJson(object)

Example:




<!DOCTYPE html>
<html>
  
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
    </script>
    <title>
      angular.toJson()
  </title>
</head>
  
<body ng-app="app" 
      style="text-align:center">
    <h1 style="color:green">
      GeeksforGeeks
  </h1>
    <h2>
      angular.toJson()
  </h2>
    <div ng-controller="geek">
        <button ng-click="showAlert()">
          Click it!</button>
    </div>
    <script>
        var app = angular.module("app", []);
        app.controller('geek', ['$scope',
                         function($scope) {
                             
            $scope.showAlert = function() {
                var string = {
                    'Name': 'Quick sort',
                    'Type': 'sorting'
                };
                alert(angular.toJson(string));
            }
        }]);
    </script>
</body>
  
</html>

Output:
Before Click:
toJson
After Click:
toJson




My Personal Notes arrow_drop_up
Recommended Articles
Page :